MATLab中不等价的循环结构 [英] Unequivalent loop structure in MATLab

查看:156
本文介绍了MATLab中不等价的循环结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个版本的代码-当第一个未执行我想要的操作时,我编写了第二个(更明确的)循环.

I have two versions of code - I wrote the second (more explicit) loop when the first didn't do what I wanted.

我哪里出错了?我怀疑有切片问题(例如,我没有正确切片数据)

Where did I go wrong? I suspect a slicing problem (as in, I'm not correctly slicing the data out)

第一个版本无法满足我的要求,但在循环上方被注释掉了:

The first version, which doesn't do what I want, is commented out above the loop:

rBool = false(h.numDirs, h.numTimes, h.numR);
for d = 1:h.numDirs
    U_first = h.data(d,1,:);
    U_first = U_first{1};
    for t = 2:h.numTimes
        U = h.data(d,t,:);
        U = U{1};
        dU = abs(U-U_first);
        %rBool(d,t,:) = (dU > (smallVal*U_first) | rBool(d,t-1));
        for r=1:h.numR
            rBool(d,t,r) = (dU(r) > (smallVal*U_first(r))| rBool(d,t-1,r));
        end
    end
end    

推荐答案

您在注释行中缺少第二个rBool的第三个索引:

You are missing the third index of the second rBool in your commented line:

rBool(d,t,:) = (dU > (smallVal*U_first) | rBool(d,t-1,:));

尽管我会像这样用括号括起来:

Although I'd parenthesize it like this:

rBool(d,t,:) = (dU > (smallVal*U_first)) | rBool(d,t-1,:);

我认为您最初隐含的版本是r==1.

The version you originally had implicitly assumed r==1, I think.

您可以通过设置来简化代码

And you can simplify your code by setting

U = h.data{d,t,1};

而不是剪切出一个细胞向量并选择第一个元素.

instead of cutting out a cell vector and choosing the first element.

这篇关于MATLab中不等价的循环结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆