TAGS :Viewed: 6 - Published at: a few seconds ago

[ merge sift descriptor matrix, numpyarray ]

I have sift descriptor matrix like this one:

 a=[[ 0.          0.          0.         ...,  0.01570028  0.11186453
   0.03728818]
 [ 0.19031648  0.18246838  0.0156962  ...,  0.06474684  0.          0.        ]
 [ 0.          0.          0.00392734 ...,  0.06480112  0.17083933
   0.04909176]
 ..., 
 [ 0.09828723  0.0176917   0.00196574 ...,  0.16905404  0.          0.        ]
 [ 0.23960059  0.18657423  0.00589182 ...,  0.00392788  0.          0.01963939]
 [ 0.00392924  0.09430183  0.15913433 ...,  0.00392924  0.01768159
   0.00589386]]
[[ 0.          0.          0.         ...,  0.01570028  0.11186453
   0.03728818]
 [ 0.19031648  0.18246838  0.0156962  ...,  0.06474684  0.          0.        ]
 [ 0.          0.          0.00392734 ...,  0.06480112  0.17083933
   0.04909176]
 ..., 
 [ 0.09828723  0.0176917   0.00196574 ...,  0.16905404  0.          0.        ]
 [ 0.23960059  0.18657423  0.00589182 ...,  0.00392788  0.          0.01963939]
 [ 0.00392924  0.09430183  0.15913433 ...,  0.00392924  0.01768159
   0.00589386]]

and i want to merge a lot of them in the fastest way...how to make it ? Do a list of all of the array's and then with .dstack ? Thanks

edit: ok .dstack doesn't work because the matrix has different dimensions...

for e.g a=[[128 values]...[128 values]], len(a)=300 and b=[[128 values]...[128 values]] len(b)=1000 result should be c=[[128 values]...[128 values]] len(c)=1300

Answer 1


Numpy convention for 2D arrays: first number is number of rows, second number is number of columns.

>>> import numpy as np
>>> a=np.zeros((2,5))
>>> a
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
>>> a.shape
(2, 5)

Use vstack to stack row wise.

>>> a=np.zeros((300,128))
>>> b=np.zeros((1000,128))
>>> a.shape
(300, 128)
>>> b.shape
(1000, 128)
>>> c=np.vstack((a,b))
>>> c.shape
(1300, 128)
>>> len(c)
1300

Dstack is really a depth stack:

>>> np.dstack((a,a)).shape
(300, 128, 2)

If you have a list of arrays:

a=[array1,array2,...,arrayn]
c=np.vstack(a)