简体   繁体   English

如何在MATLAB中将此Python嵌套的for循环转换?

[英]how to convert this Python nested for loop in MATLAB?

I need to convert the following Python nested for loop in MATLAB, but it just doesn't work. 我需要转换下面的嵌套在MATLAB中的for循环的Python,但是它不起作用。

for ii in where(obs != 0)[0]:
    ob = obs[ii]
    ncount = int(floor(top / ob))
    for jj in range(ncount):
        l_ind = max(int(floor((ob * (jj + 1) - width / 2.) / top * (G - 1))), 0)
        h_ind = min(int(ceil((ob * (jj + 1) + width / 2.) / top * (G - 1))), G - 1)
        ret[ii, l_ind : h_ind + 1] = tukey(h_ind - l_ind + 1, 0.5)

I personally don't need the "where(obs != 0)[0]" part, because my obs vector has no zeros along that dimension. 我个人不需要“ where(obs!= 0)[0]”部分,因为我的obs向量在该维度上没有零。 So far, I tried the following: 到目前为止,我尝试了以下方法:

for ii = 1:length(obs)
    ob = obs(ii);
    ncount = floor(top / ob);
    for jj = 1:ncount
        l_ind = max(floor((ob * (jj + 1) - width / 2) / top * (G - 1)), 0);
        h_ind = min(ceil((ob * (jj + 1) + width / 2) / top * (G - 1)), G - 1);
        ret(ii , l_ind : h_ind) = tukeywin(h_ind - l_ind + 1)';
    end
end

In Matlab, the tukey is called tukeywin and I don't need the extra 0.5 parameter, because it's default value. 在Matlab中,tukey称为tukeywin,我不需要额外的0.5参数,因为它是默认值。 I also had to transpose the tukeywin as you can see, to match the size, otherwise gives me another error, as well as remove the + 1 from the h_ind, probably because in Python, index starts at 0 and in Matlab, it starts at 1. If I keep the + 1, then I would have to put + 2 in the tukeywin function, to match sizes on both sides of the equation. 如您所见,我还必须转置tukeywin来匹配大小,否则会给我另一个错误,并从h_ind中删除+ 1,这可能是因为在Python中,索引从0开始,在Matlab中,索引从开始1.如果我保持+ 1,则必须在tukeywin函数中放置+ 2,以匹配方程式两边的大小。

But after this, it still doesn't work, I am having problems with that last line to generate the ret matrix. 但是在此之后,它仍然不起作用,我在生成ret矩阵的最后一行上遇到了问题。 ret matrix is initialized with zeros and is size 1972 x 1025. the obs vector is of size 1972 x 1. G is 1025, width is 300, top is 22050. The obs vector contains numbers that range between 100-600 for example, so you should be able to re-create this nested loop. ret矩阵用零初始化,大小为1972 x1025。obs向量的大小为1972 x1。G为1025,宽度为300,顶部为22050。obs向量包含范围在100-600之间的数字,例如您应该能够重新创建此嵌套循环。

It gives me error " Index in position 2 is invalid. Array indices must be positive integers or logical values ". 它给我错误“ 位置2的索引无效。数组索引必须为正整数或逻辑值 ”。 I am assuming this refers to the ret(ii , l_ind : h_ind) part, the ii is good, it will go till the length of 1972, but the l_ind : h_ind is giving me the error I think. 我假设这是指ret(ii,l_ind:h_ind)部分,ii很好,它将一直持续到1972年,但是l_ind:h_ind给了我我认为的错误。 So I am not sure how to structure this in Matlab. 所以我不确定如何在Matlab中构造它。

Without fully testing your code I think your issue is related to 0 vs 1 based indexing of arrays. 如果没有完全测试您的代码,我认为您的问题与基于0对1的数组索引有关。 You are limiting l_ind and h_ind to 0 and G-1. 您将l_indh_ind限制为0和G-1。 For Matlab you would want 1 and G. 对于Matlab,您需要1和G。

I'm not exactly sure what you are doing in the floor/ceil statements to determine your indices but I suspect the logic will need tweaked a bit. 我不确定您在floor / ceil语句中正在做什么以确定索引,但是我怀疑逻辑将需要进行一些调整。 The simplest solution is probably to just +1 the l_ind and h_ind lines. 最简单的解决方案可能是仅对l_indh_ind行进行+1。 Otherwise you need to rework them with 1-based indexing... 否则,您需要使用基于1的索引对其进行重做...

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

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