Numpy/MATLAB difference in array indexing

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Numpy/MATLAB difference in array indexing

federico vaggi-2
Hi everyone,

I was trying to port some code from MATLAB to Scipy, and I noticed a slight bug in the functionality of numpy.tile vs repmat in matlab:

For example:

a = np.random.rand(10,2)

b = tile(a[:,1],(1,5))

b.shape
Out[86]: (1, 50)

While MATLAB gives:

>>  a = rand(10,2);
>> b = repmat(a(:,1),[1,5]);
>> size(b)

ans =

    10     5

This is obviously trivial to fix**, but I'm wondering what causes the difference?  If you take a vertical slice of an array in numpy that's seen as a row vector, while in MATLAB its seen as a column vector?

Is it worth making a note in here:  http://www.scipy.org/NumPy_for_Matlab_Users ?

Federico

**
The easiest way I found was:

b = tile(a[:,1],(5,1)).T

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Numpy/MATLAB difference in array indexing

Pauli Virtanen-3
18.03.2012 23:18, federico vaggi kirjoitti:
> I was trying to port some code from MATLAB to Scipy, and I noticed a
> slight bug in the functionality of numpy.tile vs repmat in matlab:
>
> For example:
>
> a = np.random.rand(10,2)
>
> b = tile(a[:,1],(1,5))

a[:,1] is an 1-d array, and therefore considered as a (1, N) vector in
2-d context. This is not a bug --- the Numpy constructs do not always
map exactly to Matlab ones.

--
Pauli Virtanen

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Numpy/MATLAB difference in array indexing

Wes McKinney
On Sun, Mar 18, 2012 at 6:37 PM, Pauli Virtanen <[hidden email]> wrote:

> 18.03.2012 23:18, federico vaggi kirjoitti:
>> I was trying to port some code from MATLAB to Scipy, and I noticed a
>> slight bug in the functionality of numpy.tile vs repmat in matlab:
>>
>> For example:
>>
>> a = np.random.rand(10,2)
>>
>> b = tile(a[:,1],(1,5))
>
> a[:,1] is an 1-d array, and therefore considered as a (1, N) vector in
> 2-d context. This is not a bug --- the Numpy constructs do not always
> map exactly to Matlab ones.
>
> --
> Pauli Virtanen
>
> _______________________________________________
> SciPy-User mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/scipy-user

In [3]: b = tile(a[:,1:2],(1,5))

In [4]: b.shape
Out[4]: (10, 5)
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Numpy/MATLAB difference in array indexing

Sturla Molden-2
In reply to this post by Pauli Virtanen-3
On 18.03.2012 23:37, Pauli Virtanen wrote:

>> b = tile(a[:,1],(1,5))
>
> a[:,1] is an 1-d array, and therefore considered as a (1, N) vector in
> 2-d context. This is not a bug --- the Numpy constructs do not always
> map exactly to Matlab ones.

Yes.

Also, the need for "repmats" (np.repeat, np.tile, np.hstack, np.vstack)
and "reshapes" (np.reshape, np.ndarray.reshape) is less prominent in
NumPy because of broadcasting. Using MATLAB idioms like reshape and
repmat instead of broadcasting is a common mistake (or bad habit) when
coming to NumPy for MATLAB.

In my experience, 99% of cases for

   a .* reshape(b,m,n)
   a .* repmat(b,m,n)

in MATLAB will just map to NumPy constructs like these:

   a * b
   a * b[:,np.newaxis]

This, in addition to view arrays, make NumPy much more memory efficient.

Not to mention that a.T is O(1) in NumPy whereas a' is O(N*M) in MATLAB.

Sturla
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user