Appending (hstack) a comformable array to a matrix returns error
<p>I am trying to <code>np.hstack</code> an array to a matrix. The array has <code>.shape</code> <code>(a,)</code>, the matrix has <code>.shape</code> <code>(a,b)</code>, with <code>a</code> and <code>b</code> integers. I get the error that dimensions do not match (<code>all the input arrays must have same number of dimensions</code>). What should I do to <code>hstack</code> these conformable objects?</p>

<p>I think the problem is that what you are calling array is 1-dimensional whereas the matrix is 2-dimensional.</p>

<p>Let's say they are called <code>M</code> and <code>N</code>. <code>M.shape</code> gives <code>(a,)</code> (being what you called "an array") and <code>N.shape</code> gives <code>(a,b)</code> (being what you called "a matrix"). If you do <code>M = M.reshape(a, 1)</code> you get a bidimensional array with the same amount of rows as <code>N</code> and <code>np.hstack((M, N))</code> should work.</p>

<p>P.S: All the entities you mentioned are arrays, perhaps you meant vector or 1-dimensional array for the first one.</p>

<p>EDIT - For example:</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; a=  np.array([1,2,3])
&gt;&gt;&gt; a.shape
(3,)

&gt;&gt;&gt; b = np.array([[1], [2], [3]])
&gt;&gt;&gt; b.shape
(3,1)

&gt;&gt;&gt; c = np.hstack((a.reshape(3, 1), b))
&gt;&gt;&gt; c
array([[1, 1],
       [2, 2],
       [3, 3]])
&gt;&gt;&gt; c.shape
(3,2)
</code></pre>

python append