How could I Transform some lines from Matlab to Python ::

I am trying to transform the following lines into python, I already did however, I got an error not sure if I did it correct or not as I am new to python not sure about some lines: I attached both codes : In matlab (works fine):
clear all;
for ii = 1:nu_y
if (ii == 1)
t1 = 0;
t2 = p(ii);
Z(1:4) = linspace(t1,t2,4)
else
t1 = Z(3*ii-2);
t2 = t1+p(ii);
Z(3*ii-2:3*ii+1) = linspace(t1,t2,4)
end
end
In python:
import numpy as np
nu_x = 1
nu_y = 2
p = np.array([0.2,0.2])
for ii in range (nu_y):
if (ii == 1):
t1 = 0
t2 = p[ii]
Z[1,3] = np.linspace(t1,t2,3)
else:
t1 = Z[2*ii-1]
t2 = t1+p[ii]
Z[2*ii-1,2*ii+1] = linspace(t1,t2,3)

답변 (1개)

Walter Roberson
Walter Roberson 2021년 10월 10일
for ii = 1:nu_y
if (ii == 1)
That is intended to test for the first ii
for ii in range (nu_y):
if (ii == 1):
In Python, the first ii value is 0
Z(1:4) = linspace(t1,t2,4)
In MATLAB, that would be the first four entries in Z
Z[1,3] = np.linspace(t1,t2,3)
You have asked python to create a list with three elements (not 4 like you did in MATLAB), and you have asked Python to assign it to single location in a 2D array. If you were wanting to assign to the first three elements in Z then you should assigning to Z[0:2] .
It is not clear to me why you changed from 4 elements in MATLAB into 3 elements in Python.

댓글 수: 6

Thank you so much for you comment, I got your feedback, and I editied the code as the following:
import numpy as np
nu_x = 1
nu_y = 3
p = np.array([0.2,0.2,0.1])
Z=[]
for ii in range(nu_y):
if (ii == 0):
t1 = 0
t2 = p[ii]
Z[0:3] = np.linspace(t1,t2,4)
else:
t1 = Z[3*ii-2]
t2 = t1+p[ii]
Z[3*ii-2:3*ii+1] = np.linspace(t1,t2,4)
print(Z)
But still not get the same result as Matlab, have no idea for the reason,
clear all;
nu_y=3;
p=[0.2,0.2,0.1]
for ii = 1:nu_y
if (ii == 1)
t1 = 0;
t2 = p(ii);
Z(1:4) = linspace(t1,t2,4)
else
t1 = Z(3*ii-2);
t2 = t1+p(ii);
Z(3*ii-2:3*ii+1) = linspace(t1,t2,4)
end
end
Z
Z(3*ii-3:3*ii) = linspace(t1,t2,4)
I got the following error:
IndexError: list index out of range
Also, I tried to change the t1=Z(3*ii-2); to t1=Z[3*ii-1] but still error
Z[3*ii-3:3*ii] = np.linspace(t1,t2,4)
Thank you so much for your help, however still unfortunely gives wrog answer, ?
Not as Matlab ??
@Walter Roberson I attached the modified version of the code,
if you are run the same in Matlab give you different answer
import numpy as np
nu_x = 1
nu_y = 4
p = np.array([0.2,0.2,0.1,0.4])
Z=[]
for ii in range(nu_y):
if (ii == 0):
t1 = 0
t2 = p[ii]
Z[0:3] = np.linspace(t1,t2,4)
else:
t1 = Z[2*ii-2]
t2 = t1+p[ii]
Z[3*ii-3:3*ii] = np.linspace(t1,t2,4)
print(Z)
In matlab:
clear all;
nu_y=4;
p=[0.2,0.2,0.1,0.4]
for ii = 1:nu_y
if (ii == 1)
t1 = 0;
t2 = p(ii);
Z(1:4) = linspace(t1,t2,4)
else
t1 = Z(3*ii-2);
t2 = t1+p(ii);
Z(3*ii-2:3*ii+1) = linspace(t1,t2,4)
end
end
Z

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

질문:

2021년 10월 10일

댓글:

2021년 10월 11일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by