Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenate and remove duplicate element from numpy arrays

I compute an array of indices in each iteration of a loop and then I want to remove the duplicate elements and concatenate the computed array to the previous one. For example the first iteration gives me this array:

array([  1,   6,  56, 120, 162, 170, 176, 179, 197, 204])

and the second one:

array([ 29,  31,  56, 104, 162, 170, 176, 179, 197, 204]) 

and so on. How could I do it?

like image 680
Dalek Avatar asked Sep 03 '14 22:09

Dalek


1 Answers

you can concatenate arrays first with numpy.concatenate then use np.unique

import numpy as np
a=np.array([1,6,56,120,162,170,176,179,197,204])
b=np.array([29,31,56,104,162,170,176,179,197,204])
new_array = np.unique(np.concatenate((a,b),0))

print new_array

result:

[  1   6  29  31  56 104 120 162 170 176 179 197 204]
like image 129
Mazdak Avatar answered Sep 29 '22 01:09

Mazdak