Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array Operation Equivalent Python and Matlab

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
like image 778
marco Avatar asked Sep 22 '26 04:09

marco


1 Answers

import numpy as np

alpha = np.array([1e5, 1e3, 1e5, 1e7, 1e3])
dz = 0.001
zeta = alpha / dz**2
like image 87
Fred Foo Avatar answered Sep 25 '26 03:09

Fred Foo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!