2

I want to write in python the equivalent to this code in Matlab,

   zeta = zeros(1,5)
   alpha=[1e5,1e3,1e5,1e7,1e3];
   dz = 0.001;
   for i=1:length(zeta)
       zeta(i) = alpha(i)/(dz*dz);
   end

EDIT: Thank for all answers, they are all very useful and are helping understanding how python works, and seems Matlab too; because I'm not getting the full potencial of array and matrix operation. My initial programming language is C.

Now, I'm trying to figure how to code in python cycles and array operations. If you can help. (zeta is from the previous code)

nl= 7;
l=[0.3,0.1,0.2,0.1,0.1,0.1,0.3)   
wz=zeros(1,nl);         %layer width
temp=0;                 %auxiliary temp variable

for i=1:nl
    wz(i)=l(1,i)*(nz-1)+temp;
    temp=wz(1,i);
end
3
  • Can you explain what the code is supposed to do? I'm not familiar with matlab. Commented Jan 11, 2012 at 14:36
  • 1
    @marco: have a look at ipython, especially ipython -pylab . I use it instead of matlab, and it works great (unless you need some fancy toolbox). Commented Jan 11, 2012 at 14:44
  • Thanks @JakubM. When I understand the basics, probably I'll switch. I like python, and it is free. Commented Jan 11, 2012 at 16:22

5 Answers 5

6
import numpy as np

alpha = np.array([1e5, 1e3, 1e5, 1e7, 1e3])
dz = 0.001
zeta = alpha / dz**2
Sign up to request clarification or add additional context in comments.

8 Comments

Just to make a point, in python, we don't need iterate arrays?
@marco: this uses one of Numpy's vectorized idioms. You can do the same in Matlab, I think.
don't forget to add the line "zeta=np.zeros(5)" as an equivalent to "zeta = zeros(1,5)" in the question. it makes it working faster on a larger matrices
@MaxLi: I still don't get what you mean. zeta = np.zeros(5); zeta[:] = alpha / dz**2? That wouldn't be faster, would it?
Just for clarification, what @Max Li is referring to is that matlab will resize an array on demand if you try to index it beyond its size. E.g. a = 1:5; a(100) = 1; will resize a to be a 1x100 array. Therefore you need to pre-allocate arrays before iterating thorough them. In python, if you index something beyond its bounds, you'll raise an IndexError.
|
3

Translate you code to python:

alpha = [1e5,1e3,1e5,1e7,1e3]
dz = 0.001
zeta = [i/(dz**2) for i in alpha]

Comments

3

Try this, using an explicit for loop:

zeta  = [0] * 5
alpha = [1e5, 1e3, 1e5, 1e7, 1e3]
dz    = 0.001
for i in range(len(zeta)):
    zeta[i] = alpha[i] / (dz*dz)

Or this, using list comprehensions (this one should be faster):

alpha = [1e5, 1e3, 1e5, 1e7, 1e3]
dz    = 0.001
zeta  = [a/(dz*dz) for a in alpha]

Notice that I'm not using NumPy, just pure Python.

1 Comment

+1 for showing the pure Python version, though I think the OP will want to use Numpy since their coming from Matlab.
3

Just to make sure everything is touched on, when you're using Matlab, you should always vectorize, and that wasn't done in the initial code. Therefore, I would recommend that:

zeta = zeros(1,5)
alpha=[1e5,1e3,1e5,1e7,1e3];
dz = 0.001;
for i=1:length(zeta)
    zeta(i) = alpha(i)/(dz*dz);
end

Actually be written as:

alpha = [1e5,1e3,1e5,1e7,1e3];
dz = 0.001;
zeta = alpha/dz^2;

Then the code that you write in numpy follows much more naturally from what you've written in Matlab and likewise takes advantage of vectorization.

import numpy
alpha = numpy.array([1e5,1e3,1e5,1e7,1e3])
dz = 0.001
zeta = alpha/(dz*dz)

In both cases, there's no need to pre-allocate.

ADDITION AFTER EDIT: So I think there are still some errors in the first block. You had:

nl= 7;
l=[0.3,0.1,0.2,0.1,0.1,0.1,0.3)   
wz=zeros(1,nl);         %layer width
temp=0;                 %auxiliary temp variable

for i=1:nl
  wz(i)=l(1,i)*(nz-1)+temp;
  temp=wz(1,i);
end

You never defined nz, so I am guessing you meant the scalar nl, which (I think) is supposed to be the length of l (or was this just a coincidence?). Just for the first part, I'm guessing this is what you would like to do:

l=[0.3,0.1,0.2,0.1,0.1,0.1,0.3];
nl = length(l);
wz = cumsum(l*(nl-1));

This is of course the Matlab version, and the numpy version also follows naturally where:

l = numpy.array([0.3,0.1,0.2,0.1,0.1,0.1,0.3])
n1 = len(l)
wz = numpy.cumsum(l*(n1-1))

I would like to clarify that this was what you wanted for the first part before I take a stab at the second.

5 Comments

Thank you, your answer is the key. I didn't know the correct way in matlab, so I didn't find in python. I edited my question. If you can help, I'll apreciate. With 2 or 3 examples I think that I'm able to learn how to program in python (and Matlab)
Regarding your "new" post: (1) You pre-allocate a matrix A and then don't do anything with it. (2) you index a variable l(1,i) that I don't see you defining. Did you mean for that to be b(1,i)?
I'm sorry @Factor3 . I paste in the wrong code block. Matrix A is to second code. And b is not necessary (now). I'll edit question and put everything alright
I'm sorry @Factor3, nz is a parameter, "number of points which represents z dimension". In this case nz=101. l is an array which aggregates layers' dimensions. wz is an array which aggregate the number of points through the "width". It's wierd, but is necessary to solve the following matrixes (second part)
I noticed, with my "new nz" the code is the same, to solve this particular problem. Thank you ! BUt, about the second part? Is it possible elimite for cycles?
2

With numpy, as with matlab or similar languages, you actually don't want to use loops over indicies when you can use array-based operations, which are faster and clearer:

import numpy
zeta = numpy.zeros((1,5))
alpha = numpy.array([1e5,1e3,1e5,1e7,1e3])
dz = 0.001
zeta[0,:] = alpha/(dz*dz)

if you really wanted to loop over the indicies, you'd do this:

import numpy
zeta = numpy.zeros((1,5))
alpha = numpy.array([1e5,1e3,1e5,1e7,1e3])
dz = 0.001
for i in xrange(alpha.size):
...     zeta[0,i] = alpha[i]/(dz*dz)

1 Comment

Thank you very much, it's very useful to understand the main differences!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.