# [SciPy-user] Mathematica Element-wise Multiplication

David Cournapeau david@ar.media.kyoto-u.ac...
Sun Dec 16 20:28:48 CST 2007

```Johann Cohen-Tanugi wrote:
> Actually I do not manage to use .T or .transpose() method on 1D arrays :
>
> In [42]: a = array([[ 0.0, 0.0,
> 0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])   <--this is
> example 3 of thiis indeed very nice tutorial on broadcasting
> In [43]: b = array([1.0,2.0,3.0,4.0])
> In [44]: a+b
> ValueError: shape mismatch: objects cannot be broadcast to a single
> shape    <----- fine, mismatch of trailing dimensions
>
> In [45]: b.transpose()
> Out[45]: array([ 1.,  2.,  3.,  4.])     <------ ominous : no change
This is confusing if you are coming from matlab and similar softwares
(it was for me, at least). In matlab, any array is rank 2 by default
(let's put aside rank > 2 for now). That is, in matlab, a = [1, 2, 3]
gives you a row array, which is interpreted as a matrix: size(a) gives
you [1, 3]. In numpy, this is not the case: there is a real difference
between "row arrays", "column arrays" and vectors.

a = array([1, 2, 3])
a.ndim <----------- is 1 (rank 1)
a = array([[1], [2], [3]])
a.ndim <----------- is 2 (rank 2: column)
a = array([[1, 2, 3]])
a.ndim <----------- is 2 (rank 2: row)

In Matlab, there is no such difference, and it is really ingrained in
the software (for example, at the C api level, the function to get the
number of dimensions, alas the 'rank', always returns at least 2). To
solve your problem, you should have a new dimension:

b = array([1., 2, 3])
b.ndim <----------- 1
b[:, numpy.newaxis]
b.ndim <----------- 2 (this will be a column array: b is now exactly the
same as array([[1.], [2], [3]])
b = array([1., 2, 3])
b[numpy.newaxis, :]
b.ndim <----------- 2 (this will be a row array: b is now exactly the
same as array([[1., 2, 3]])

cheers,

David
```