将python代码转换为MATLAB

2024-04-29 20:10:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在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转换尝试:

^{pr2}$

X2y2x4y4绝对不正确,但我不明白这里的意思x3 [0] -x1 [-1])。。。所以在MATLAB中,数组以x3(1)开头,而不是{},据我所知,没有任何东西像{}。 有人能给我解释一下,也许能建议我如何重写x2y2x4y4。在


Tags: nppicvinnerx1outerphimatlab
2条回答

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

语法x[-1]索引Python中数组中的最后一个元素。在

要在MATLAB中实现这一点,可以使用end关键字

x(end)

相关问题 更多 >