I have this code in MATLAB and I am trying to convert it in Python.
M=zeros(1,N);
i=1;
while i<=N
ind=mod(p*(i-1)+1,N);
if ind==0
ind=N;
end
while M(ind)~=0
ind=ind+1;
end
M(ind)=i;
i=i+1;
ind=ind+1;
end
display(M);
M1=zeros(m,2/n_lay*n_wc);
for i=1:m
M1(i,:)=M(2/n_lay*n_wc*(i-1)+1:2/n_lay*n_wc*i);
end
I don't know how to convert the for loop and what I have until now is the code below, and I don't know exactly how to convert the line "M1(i,:) = M(2/n_layn_wc*(i-1)+1:2/n_layn_wci)*" here is the probleme where I get from Python "Invalid syntax".
import numpy, scipy, matplotlib
N = 24
p = 2
n_lay = 2
n_wc=1
M=zeros(1,N)
i=1;
while i<=N:
ind=mod(p*(i-1)+1,N)
if ind==0 :
ind=N
end
while M(ind)!=0:
ind=ind+1
end
M(ind)=i
i=i+1
ind=ind+1
end
display(M)
M1=zeros(m,2/n_lay*n_wc)
for i in range (1,m):
M1(i,:) = M(2/n_lay*n_wc*(i-1)+1:2/n_lay*n_wc*i)
end
There are lot of syntax errors and other mistakes in your converted code. All arrays should be converted from round brackets to square. Though this does not work, your code should look somewhat like this:
import numpy, scipy, matplotlib
N = 24
p = 2
n_lay = 2
n_wc=1
M=[]
i=1;
while i<=N:
ind=(p*(i-1)+1)%N
if ind==0 :
ind=N
while M[ind]!=0:
ind=ind+1
M[ind]=i
i=i+1
ind=ind+1
M1=[]
for i in range (1,M):
M1[i,:] = M[2/n_lay*n_wc*(i-1)+1:2/n_lay*n_wc*i]
Also if you want to update add elements to an array, you should use "array.append(element)".