Re: Numeric and matlab
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Numeric and matlab
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(a5); a(idx)=6; you can do: cond=a5; a(cond) = 6; or even shorter a(a5) = 6; Does someone know if the same trick is possible in NumPy? Cheers, Bas -- http://mail.python.org/mailman/listinfo/python-list
Re: Numeric and matlab
Robert Kern wrote: A better place to ask would be [EMAIL PROTECTED] . By the way, Numeric has undergone a rewrite and is now known as numpy. thanks for the pointer! it is a bit confusing with all of the different numerical modules (Numeric, numpy, scipy, ScientificPython, numarray, etc...) to keep them all straight. importing numpy works quite well, and does all of the examples that you provided. thanks! bb -- - [EMAIL PROTECTED] http://web.bryant.edu/~bblais -- http://mail.python.org/mailman/listinfo/python-list
Re: Numeric and matlab
Bas wrote: 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(a5); a(idx)=6; you can do: cond=a5; a(cond) = 6; or even shorter a(a5) = 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[a5]=6 bb -- - [EMAIL PROTECTED] http://web.bryant.edu/~bblais -- http://mail.python.org/mailman/listinfo/python-list
Numeric and matlab
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(a5); a(idx)=6; % extract elements idx=find(b0) 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 -- - [EMAIL PROTECTED] http://web.bryant.edu/~bblais -- http://mail.python.org/mailman/listinfo/python-list
Re: Numeric and matlab
Brian Blais wrote: Hello, Most of my experience is with Matlab/Octave, so I am a Python newbie (but enjoying it! :) ) A better place to ask would be [EMAIL PROTECTED] . By the way, Numeric has undergone a rewrite and is now known as numpy. http://numeric.scipy.org 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; In [18]: from numpy import * In [19]: a = arange(1,11) In [20]: a Out[20]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) In [22]: b = arange(-5, 5.1, 0.1) In [23]: b Out[23]: array([ -5.e+00, -4.9000e+00, -4.8000e+00, -4.7000e+00, -4.6000e+00, -4.5000e+00, -4.4000e+00, -4.3000e+00, -4.2000e+00, -4.1000e+00, -4.e+00, -3.9000e+00, -3.8000e+00, -3.7000e+00, -3.6000e+00, -3.5000e+00, -3.4000e+00, -3.3000e+00, -3.2000e+00, -3.1000e+00, -3.e+00, -2.9000e+00, -2.8000e+00, -2.7000e+00, -2.6000e+00, -2.5000e+00, -2.4000e+00, -2.3000e+00, -2.2000e+00, -2.1000e+00, -2.e+00, -1.9000e+00, -1.8000e+00, -1.7000e+00, -1.6000e+00, -1.5000e+00, -1.4000e+00, -1.3000e+00, -1.2000e+00, -1.1000e+00, -1.e+00, -9.e-01, -8.e-01, -7.e-01, -6.e-01, -5.e-01, -4.e-01, -3.e-01, -2.e-01, -1.e-01, -1.77635684e-14, 1.e-01, 2.e-01, 3.e-01, 4.e-01, 5.e-01, 6.e-01, 7.e-01, 8.e-01, 9.e-01, 1.e+00, 1.1000e+00, 1.2000e+00, 1.3000e+00, 1.4000e+00, 1.5000e+00, 1.6000e+00, 1.7000e+00, 1.8000e+00, 1.9000e+00, 2.e+00, 2.1000e+00, 2.2000e+00, 2.3000e+00, 2.4000e+00, 2.5000e+00, 2.6000e+00, 2.7000e+00, 2.8000e+00, 2.9000e+00, 3.e+00, 3.1000e+00, 3.2000e+00, 3.3000e+00, 3.4000e+00, 3.5000e+00, 3.6000e+00, 3.7000e+00, 3.8000e+00, 3.9000e+00, 4.e+00, 4.1000e+00, 4.2000e+00, 4.3000e+00, 4.4000e+00, 4.5000e+00, 4.6000e+00, 4.7000e+00, 4.8000e+00, 4.9000e+00, 5.e+00]) % set all the values above 5 equal to 6 idx=find(a5); a(idx)=6; In [24]: a[a5] = 6 In [25]: a Out[25]: array([1, 2, 3, 4, 5, 6, 6, 6, 6, 6]) % extract elements idx=find(b0) b=b(idx); In [27]: b[b0] Out[27]: array([ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. ]) % meshgrid...usually to go through all possibly values of a parameter [x,y]=meshgrid(1:10,-5:.1:5) In [28]: x,y = mgrid[1:11, -5:5.1:0.1] -- Robert Kern [EMAIL PROTECTED] In the fields of hell where the grass grows high Are the graves of dreams allowed to die. -- Richard Harter -- http://mail.python.org/mailman/listinfo/python-list
Re: Numeric and matlab
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 class X(object): ... def __getitem__(self,i): ... return self.lst[i] ... def __init__(self): ... self.lst = [7,5,3,1] ... x=X() x[0] 7 x[1] 5 or consider class List(list): ... def __getitem__(self,idx): ... if type(idx) is list: ... ret = [] ... lst = list(self) ... return [lst[i] 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)[s] 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 -- http://mail.python.org/mailman/listinfo/python-list