I’ve got three different numpy arrays
a = array([ 0, 3, 6, 9, 12])
b = array([ 1, 4, 7, 10, 13])
c = array([ 2, 5, 8, 11, 14])
How can I join them using numpy methods that
d = array[(0,1,2,3,4,...,12,13,14)]
I don’t want to write a loop like
for i in range(len(a)):
[...]
This is only an example in my project the arrays are not sorted and I want to keep their order.
You can transpose and flatten the arrays:
An alternative way to combine the arrays is to use
numpy.vstack():(I don’t know which one is faster, by the way.)
Edit: In response to the answer by Nicolas Barbey, here is how to make do with copying the data only once:
This code ensures that the data is layed out in a way that
ravel()does not need to make a copy, and indeed it is quite a bit faster than the original code on my machine: