[SciPy-User] a new enigma for a matlab user

Sturla Molden sturla at molden.no
Mon May 23 13:17:35 EDT 2011


Den 23.05.2011 14:14, skrev midel:
> Thanks ! ok, so :
>
> variable1=variable2;
>
> creates a pointer, not an independant and new variable ?

It creates a new variable, but not a new object.

Observe that this also happens for sub-arrays of NumPy array:

    a = b[::2]

means that the name 'a' points to every second element in b. So changing 
a[n] will also change b[2*n]. On the other hand, Python lists does not 
create views.

This is one of the strengths of Python and NumPy, particularly when 
working with large data sets. But if you are used to MATLAB, it is 
terrible to get used to. Python and NumPy is not a free clone of MATLAB. 
It is very different, albeit some parts might look similar at first sight.

Another thing that might surprise you is appending to a list. In Matlab, 
appending n elements to an array is O(N**2), in Python appending to a 
list is O(N). So in Matlab you will always preallocate an array to store 
data, whereas in Python you can just let a list grow while you read new 
data.

Another difference is that a NumPy array can reference any "memory" (a 
memory mapped file, shared memory, an image from PIL, whatever). In 
MATLAB you always get a copy.

Sturla








More information about the SciPy-User mailing list