Given two sorted arrays like the following:
a = array([1,2,4,5,6,8,9])
b = array([3,4,7,10])
I would like the output to be:
c = array([1,2,3,4,5,6,7,8,9,10])
or:
c = array([1,2,3,4,4,5,6,7,8,9,10])
I’m aware that I can do the following:
c = unique(concatenate((a,b))
I’m just wondering if there is a faster way to do it as the arrays I’m dealing with have millions of elements.
Any idea is welcomed. Thanks
Since you use numpy, I doubt that bisec helps you at all… So instead I would suggest two smaller things:
np.sort, usec.sort()method instead which sorts the array in place and avoids the copy.np.uniquemust usenp.sortwhich is not in place. So instead of usingnp.uniquedo the logic by hand. IE. first sort (in-place) then do thenp.uniquemethod by hand (check also its python code), withflag = np.concatenate(([True], ar[1:] != ar[:-1]))with whichunique = ar[flag](with ar being sorted). To be a bit better, you should probably make the flag operation in place itself, ie.flag = np.ones(len(ar), dtype=bool)and thennp.not_equal(ar[1:], ar[:-1], out=flag[1:])which avoids basically one full copy offlag..sorthas 3 different algorithms, since your arrays maybe are almost sorted already, changing the sorting method might make a speed difference.This would make the full thing close to what you got (without doing a unique beforehand):