Martin Böschen
Martin Böschen

Reputation: 1769

hstack throws ValueError: wrong dimensions

I try to concat two matrices along the columns (as [a,b] in matlab). But I'm keep getting the error:

ValueError: arrays must have same number of dimensions

This my code:

print a.shape
print b.shape 
X= np.hstack([b,a])

And this is the output:

(223129, 1)
(223129, 4)

It my understanding, the dimensions are fine. What is going wrong here?

Upvotes: 0

Views: 571

Answers (2)

Martin Böschen
Martin Böschen

Reputation: 1769

Ok, I found my error. One of my matrix was a sparse matrix, and then you get the error I had.

Upvotes: 1

Lee
Lee

Reputation: 31040

a and b have different number of dimensions. Do a.ndim or b.ndim. The results should be equal.

If a and b have the dimensions that you seem to indicate, then it should work.

e.g.

a=np.ones((5,1))
b=np.ones((5,4))
np.hstack([b,a])
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])

It works for me if I use the same shapes {(223129,1) and (223129,4)} for a and b that you indicate.

Upvotes: 1

Related Questions