Numeric and matlab

B

Brian Blais

Hello,

Most of my experience is with Matlab/Octave, so I am a Python newbie (but enjoying
it! :) )

There are a lot of things that I do in Matlab that I'd like to know the proper way to
do in Python. Here are a few:

MATLAB:

% example vectors
a=1:10;
b=-5:.1:5;

% set all the values above 5 equal to 6
idx=find(a>5);
a(idx)=6;

% extract elements

idx=find(b>0)
b=b(idx);

% meshgrid...usually to go through all possibly values of a parameter

[x,y]=meshgrid(1:10,-5:.1:5)
x=x:)); y=y:));
for i=1:length(x)
% do something with x(i) and y(i)
end


I'm sure there are more, but these jump out at me as I'm going. It seems as if the
idx=find() stuff can be done with Numeric.nonzeros(), but you can't index with that, like

a=Numeric.arange(1,11,1)
idx=Numeric.nonzeros(a)
a=a[idx] % doesn't work

and what about meshgrid? Do I use the fromfunction() in some way? Is there a
resource that goes through comparisons like this?


thanks,


Brian Blais
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

Hello,

[...]
I'm sure there are more, but these jump out at me as I'm going. It
seems as if the idx=find() stuff can be done with Numeric.nonzeros(),
but you can't index with that, like

a=Numeric.arange(1,11,1)
idx=Numeric.nonzeros(a)

import Numeric as N
N.nonzero

without s :)
a=a[idx] % doesn't work

i don't know whether arange object overloads __getitem__
like
.... def __getitem__(self,i):
.... return self.lst
.... def __init__(self):
.... self.lst = [7,5,3,1]
....
>>> x=X()
>>> x[0] 7
>>> x[1] 5
>>>

or consider
.... def __getitem__(self,idx):
.... if type(idx) is list:
.... ret = []
.... lst = list(self)
.... return [lst for i in range(len(lst)) if i in idx]
....
>>> lst=List([1,2,3,4,5])
>>> lst [1, 2, 3, 4, 5]
>>> lst[[0,2]] [1, 3]
>>>

if it would overload this would be possible, the normal
python list takes slices

range(0,100)[1:10:2]
s = slice(1,10,2)
range(0,100)

here some ideas for what you tring to do
>>> nums # normal python list [0, 1, 2, 1, 0, 0, 0, 3, 4, 0]
>>> filter(lambda x: x!=0, nums)
[1, 2, 1, 3, 4]
>>> [item for item in nums if item !=0 ] [1, 2, 1, 3, 4]
>>>
>>> a=N.arange(0,11,.5)
>>> filter(lambda x: x!=0, a)
[0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0,
7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5]
>>> [item for item in a if item !=0 ]
[0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0,
7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5]

Regards, Daniel
 
B

Bas

Hi,

I am also considering a switch from Matlab to NumPy/SciPy at some
point.

Note that in the last version of Matlab (7?) you don't have to use
'find', but you now can 'conditional arrays' as an index, so instead
of
idx=find(a>5);
a(idx)=6;
you can do:
cond=a>5;
a(cond) = 6;
or even shorter
a(a>5) = 6;

Does someone know if the same trick is possible in NumPy?

Cheers,
Bas
 
B

Brian Blais

Bas said:
I am also considering a switch from Matlab to NumPy/SciPy at some
point.

Note that in the last version of Matlab (7?) you don't have to use
'find', but you now can 'conditional arrays' as an index, so instead
of
idx=find(a>5);
a(idx)=6;
you can do:
cond=a>5;
a(cond) = 6;
or even shorter
a(a>5) = 6;

Does someone know if the same trick is possible in NumPy?

A response I got back from someone else is, yes. I tested it, and it works in numpy.
You can do:

import numpy
a=numpy.arange(0,10,.1)
a[a>5]=6


bb
 
M

malv

A convincing experience is to 'translate' some substantial matlab
matrix code into python.
You will at once see the difference between a true programming language
and matlab

Further, also look at matplotlib.
malv
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top