[英]Converting a python code to MATLAB
我正在尝试在MATLAB中重写一些python代码,该python代码如下所示:
theta = 0:pi/510:2*pi;
phi = pi/3:2*pi/360:6*pi;
phi_outer = np.linspace(CV.Outer.phi_min, CV.Outer.phi_max, 100)
phi_inner = np.linspace(CV.Inner.phi_max, CV.Inner.phi_min, 100)
x1, y1 = coords(phi_outer, geo, theta, CV.Outer.involute)
t = np.linspace(0,1,100)
x3, y3 = coords(phi_inner, geo, theta, CV.Inner.involute)
x2 = (x3[0]-x1[-1])*t+x1[-1]
y2 = (y3[0]-y1[-1])*t+y1[-1]
x4 = (x1[0]-x3[-1])*t+x3[-1]
y4 = (y1[0]-y3[-1])*t+y3[-1]
我尝试进行MATLAB转换:
theta = 0:pi/510:2*pi;
phi = pi/3:2*pi/360:6*pi;
phi_outer = linspace(CV.Outer.phi_min, CV.Outer.phi_max, 100);
phi_inner = linspace(CV.Inner.phi_max, CV.Inner.phi_min, 100);
[x1, y1] = coords(phi_outer, geo, theta, CV.Outer.involute);
[x3, y3] = coords(phi_inner, geo, theta, CV.Inner.involute);
t = linspace(0,1,100);
x2 = (x3(2)-x1(1)).*t+x1(1);
y2 = (y3(2)-y1(1)).*t+y1(1);
x4 = (x1(2)-x3(1)).*t+x3(1);
y4 = (y1(2)-y3(1)).*t+y3(1);
X2
, y2
, x4
, y4
绝对不正确,但是我不明白这里的意思x3 [0] -x1 [-1]
)...因此在MATLAB中,数组以x3(1)
开头而不是x3(0)
而据我所知,没有类似x1(-1)
东西。 有人可以向我解释一下,也许可以建议我如何重写x2
, y2
, x4
, y4
。
语法x[-1]
索引Python中数组中的最后一个元素 。
要在MATLAB中实现此目的,可以使用end
关键字
x(end)
MATLAB索引从1
开始,而不是像python那样从0
开始。 所以会
x2 = (x3(1)-x1(end)).*t+x1(end);
y2 = (y3(1)-y1(end)).*t+y1(end);
x4 = (x1(1)-x3(end)).*t+x3(end);
y4 = (y1(1)-y3(end)).*t+y3(end);
基本上每个元素的索引都为MATLABindex = PythonIndex-1
,并且,正如其他人已经提到的那样,MATLAB选择数组中最后一个元素的方式是end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.