Iterating over arrays to create vectors: Python Vs MATLAB
So I have this code in MATLAB:
dx = 1E-2;
u = [0:0.01:1000]%g(:,4);
minu= min(u);
maxu = max(u);
du = dx*1.5; % change back
p1 = floor((maxu-minu)/du)+1;
N1 = zeros(p1,1);
for i = 1:size(u,1)
U1 = floor((u(i)-minu)/du) + 1; %seems to either only be 0 or 1??
N1(U1) = N1(U1)+1;
end
L = (N1/(p1*du));
plot(tan(L))
grid on
Which produces
[plot][1]
[1]: https://i.stack.imgur.com/6BJFj.png
I tried to transfer this over to Python with:
# %%
import numpy as np
import scipy as sc
import matplotlib.pyplot as plt
# from mpl_toolkits import mplot3d
import cmath
from scipy.fft import fft
from scipy.fft import ifft
# import sympy as sy
# import sympy.abc as spa
# from mpl_toolkits import mplot3d
dx = 1E-2;
u = np.arange(0, 1000, 0.1)
minu= min(u);
maxu = max(u);
du = dx*1.5
p1 = np.floor((maxu-minu)/du)+1;
N1 = np.zeros((int(p1), 1))
for i in range(1, len((u))):
U1 = np.floor( (u - minu) / du ) + 1
N1[U1] = N1[U1]+1
L = (N1/(p1*du));
plt.plot(np.tan(L))
But it fails at the line `N1[U1] = N1[U1]+1` because I'm not familiar with the syntax in Python, how might one replicate the line `N1(U1) = N1(U1)+1;` in Python?
I appreciate that this is a basic question to ask but I'm a newcommer to the language.
Posted May 1, 2022, 9:49 PM UTC