Numeric and matlab

Robert Kern robert.kern at gmail.com
Sun Feb 5 17:20:16 EST 2006


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 numpy-discussion at lists.sourceforge.net . 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.00000000e+00,  -4.90000000e+00,  -4.80000000e+00,
        -4.70000000e+00,  -4.60000000e+00,  -4.50000000e+00,
        -4.40000000e+00,  -4.30000000e+00,  -4.20000000e+00,
        -4.10000000e+00,  -4.00000000e+00,  -3.90000000e+00,
        -3.80000000e+00,  -3.70000000e+00,  -3.60000000e+00,
        -3.50000000e+00,  -3.40000000e+00,  -3.30000000e+00,
        -3.20000000e+00,  -3.10000000e+00,  -3.00000000e+00,
        -2.90000000e+00,  -2.80000000e+00,  -2.70000000e+00,
        -2.60000000e+00,  -2.50000000e+00,  -2.40000000e+00,
        -2.30000000e+00,  -2.20000000e+00,  -2.10000000e+00,
        -2.00000000e+00,  -1.90000000e+00,  -1.80000000e+00,
        -1.70000000e+00,  -1.60000000e+00,  -1.50000000e+00,
        -1.40000000e+00,  -1.30000000e+00,  -1.20000000e+00,
        -1.10000000e+00,  -1.00000000e+00,  -9.00000000e-01,
        -8.00000000e-01,  -7.00000000e-01,  -6.00000000e-01,
        -5.00000000e-01,  -4.00000000e-01,  -3.00000000e-01,
        -2.00000000e-01,  -1.00000000e-01,  -1.77635684e-14,
         1.00000000e-01,   2.00000000e-01,   3.00000000e-01,
         4.00000000e-01,   5.00000000e-01,   6.00000000e-01,
         7.00000000e-01,   8.00000000e-01,   9.00000000e-01,
         1.00000000e+00,   1.10000000e+00,   1.20000000e+00,
         1.30000000e+00,   1.40000000e+00,   1.50000000e+00,
         1.60000000e+00,   1.70000000e+00,   1.80000000e+00,
         1.90000000e+00,   2.00000000e+00,   2.10000000e+00,
         2.20000000e+00,   2.30000000e+00,   2.40000000e+00,
         2.50000000e+00,   2.60000000e+00,   2.70000000e+00,
         2.80000000e+00,   2.90000000e+00,   3.00000000e+00,
         3.10000000e+00,   3.20000000e+00,   3.30000000e+00,
         3.40000000e+00,   3.50000000e+00,   3.60000000e+00,
         3.70000000e+00,   3.80000000e+00,   3.90000000e+00,
         4.00000000e+00,   4.10000000e+00,   4.20000000e+00,
         4.30000000e+00,   4.40000000e+00,   4.50000000e+00,
         4.60000000e+00,   4.70000000e+00,   4.80000000e+00,
         4.90000000e+00,   5.00000000e+00])

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

In [24]: a[a>5] = 6

In [25]: a
Out[25]: array([1, 2, 3, 4, 5, 6, 6, 6, 6, 6])

> 	% extract elements
> 
> 	idx=find(b>0)
> 	b=b(idx);

In [27]: b[b>0]
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
robert.kern at gmail.com

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list