-4

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
amahmud
  • 434
  • 4
  • 14
Iulia_Vascan
  • 87
  • 3
  • 10

1 Answers1

0

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)".

kkblue
  • 183
  • 10
  • I have an error when I run this code, it is "_TypeError: can only concatenate list (not "int") to list_" – Iulia_Vascan Jul 26 '18 at 12:09
  • There was a problem in one of the brackets. But there is a infinite loop being created at line number 8 that you might have to fix. – kkblue Jul 26 '18 at 15:14