I'm a C and MATLAB user. When I started learning Python (a week ago) I noticed that I don't use full potential of MATLAB, in particular array operations. I use for loops often, probably because I learnt programming in C.
In a previous tip, I learnt to use cumsum and other efficient array operations, for example:
alpha = [1e-4,1e-3,1e-4,1e-1,1e-2,1e-3,1e-6,1e-3];
zeta = alpha / (dz*dz)
nz = 101
l=[0.3,0.1,0.2,0.1,0.1,0.1,0.2];
wz = cumsum(l*(nz-1));
nl = lenght(l);
Is it possible simplify the following code in Python (Numpy) or MATLAB?
A = zeros(nz,nz);
i=1;
for j = 2:wz(i)-1
A(j,j-1) = zeta(1,1);
A(j,j) = -2*zeta(1,1);
A(j,j+1) = zeta(1,1); % layer 1 nodes
end
%cicle to n-layers
for i=2:nl
for j=wz(i-1):wz(i-1)
A(j,j-1) = zeta(1,i-1);
A(j,j) = -zeta(1,i-1)-zeta(1,i);
A(j,j+1) = zeta(1,i);
end
for j=wz(i-1)+1:wz(i)
A(j,j-1) = zeta(1,i);
A(j,j) = -2*zeta(1,i);
A(j,j+1) = zeta(1,i);
end
end
end