简体   繁体   English

将带有数组代码的MatLab for循环转换为python

[英]Converting MatLab for loop with array code to python

I was given code in Matlab made by someone else and asked to convert to python. However, I do not know MatLab.This is the code:我在别人制作的 Matlab 中得到了代码,并要求转换为 python。但是,我不知道 MatLab。这是代码:

for i = 1:nWind因为我 = 1:nWind

[input(a:b,:), t(a:b,1)] = EulerMethod(A(:,:,:,i),S(:,:,i),B(:,i),n,scale(:,i),tf,options);

fprintf("%d\n",i);

for j = 1:b
        vwa = generate_wind([input(j,9);input(j,10)],A(:,:,:,i),S(:,:,i),B(:,i),n,scale(:,i));
        wxa(j) = vwa(1);
        wya(j) = vwa(2);
end


% Pick random indexes for filtered inputs
rand_index = randi(tf/0.01-1,1,filter_size);
inputf(c:d,:) = input(a+rand_index,:);
wxf(c:d,1) = wxa(1,a+rand_index);
wyf(c:d,1) = wya(1,a+rand_index);
wzf(c:d,1) = 0;

I am confused on what [input(a:b,:), t(a:b,1)] mean and if wxf, wzf, wyf are part of the MatLab library or if it's made.我对 [input(a:b,:), t(a:b,1)] 的含义以及 wxf、wzf、wyf 是否是 MatLab 库的一部分或是否已创建感到困惑。 Also, EulerMethod and generate_wind are seprate classes.此外,EulerMethod 和 generate_wind 是单独的类。 Can someone help me convert this code to python?有人可以帮我将此代码转换为 python 吗?

The only thing I really changed so far is changing the for loop from:到目前为止我真正改变的唯一一件事是改变 for 循环:

for i = 1:nWind因为我 = 1:nWind

to

for i in range(1,nWind):对于我在范围内(1,nWind):

There's several things to unpack here.这里有几件事情要解包。

First, MATLAB indexing is 1-based, while Python indexing is 0-based.首先,MATLAB 索引是从 1 开始的,而 Python 索引是从 0 开始的。 So, your for i = 1:nWind from MATLAB should translate to for i in range(0,nWind) in Python (with the zero optional).因此,您的for i = 1:nWind from MATLAB 应转换为for i in range(0,nWind) in Python(零可选)。 For nWind = 5, MATLAB would produce 1,2,3,4,5 while Python range would produce 0,1,2,3,4.对于 nWind = 5,MATLAB 将产生 1,2,3,4,5,而 Python range将产生 0,1,2,3,4。

Second, wxf , wyf , and wzf are local variables.其次, wxfwyfwzf是局部变量。 MATLAB is unique in that you can assign into specific indices at the same time variables are declared. MATLAB 的独特之处在于您可以在声明变量的同时分配给特定的索引。 These lines are assigning the first rows of wxa and wya (since their first index is 1) into the first columns of wxf and wyf (since their second index is 1).这些行将wxawya的第一行(因为它们的第一个索引是 1)分配到wxfwyf的第一列(因为它们的第二个索引是 1)。 MATLAB will also expand an array if you assign past its end. MATLAB 也将扩展一个数组,如果你分配超过它的末尾。

Without seeing the rest of the code, I don't really know what c and d are doing.没有看到代码的rest,真不知道cd在干什么。 If c is initialized to 1 before the loop and there's something like c = d+1;如果c在循环之前被初始化为 1 并且有类似c = d+1; later, then it would be that your variables wxf , wyf , and wzf are being initialized on the first iteration of the loop and expanded on later iterations.稍后,您的变量wxfwyfwzf将在循环的第一次迭代中初始化,并在以后的迭代中扩展。 This is a common (if frowned upon) pattern in MATLAB. If this is the case, you'd replicate it in Python by initializing to an empty array before the loop and using the array's extend() method inside the loop (though I bet it's frowned upon in Python, as well).这是 MATLAB 中的常见(如果不赞成)模式。如果是这种情况,您可以通过在循环之前初始化为空数组并在循环内使用数组的extend()方法来在 Python 中复制它(尽管我敢打赌它在 Python 中也不受欢迎)。 But really, we need you to edit your question to include a , b , c , and d if you want to be sure this is really the case.但实际上,我们需要您编辑您的问题以包括abcd如果您想确定情况确实如此。

Third, EulerMethod and generate_wind are functions, not classes.第三, EulerMethodgenerate_wind是函数,不是类。 EulerMethod returns two outputs, which you'd probably replicate in Python by returning a tuple. EulerMethod返回两个输出,您可能会通过返回一个元组在 Python 中复制它们。

[input(a:b,:), t(a:b,1)] = EulerMethod(...); is assigning the two outputs of EulerMethod into specific ranges of input and t .EulerMethod的两个输出分配到inputt的特定范围。 Similar concepts as in points 1 and 2 apply here.与第 1 点和第 2 点类似的概念适用于此。

Those are the answers to what you expressed confusion about.这些是您表达困惑的答案。 Without sitting down and doing it myself, I don't have enough experience in Python to give more Python-specific recommendations.没有坐下来自己做,我在 Python 没有足够的经验来给出更多 Python 特定的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM