【问题标题】:Issue Using Multiple Criteria in If-Statements in MATLAB在 MATLAB 的 If 语句中使用多个条件的问题
【发布时间】:2019-02-20 22:02:47
【问题描述】:

我遇到了一个问题,对于第一个 for 循环,我在 no2_iterate(1) 处获得了我想要的输出,但在生成 no2_iterate(2) 的第二个循环中,我没有得到任何输出。

以下是我生成 no2_iterate(1) & (2) 的两个 if 语句/for 循环。

no2_sum_1cm = 0;
gridh_iterate = 0 % starting height in cm
lato = 1;
lono = 1;
no2_iterate_start = 0;
no2_iterate(1:2) = 0;

if gridh_iterate < gridh(lato,lono,1);
    no2_layer = no2_moleccm3(lato,lono,1,12);
    for i = 1:gridh(lato,lono,1);
        for h = 1;
            gridh_iterate = gridh_iterate+ 1; % in cm, now compare to gridh(1,1,1) and the other areas, so if its over the height of the cell switch no2 concn
        end
        no2_iterate(1) = (no2_iterate(1) + no2_layer)*1; % Now units of g no2/cm2
    end
    no2_iterate = no2_iterate
end

if gridh_iterate < gridh(lato,lono,2) && gridh_iterate >gridh(lato,lono,1);
    no2_layer = no2_moleccm3(lato,lono,2,12);
    for i = 1:gridh(lato,lono,2);
        for h = 1;
            gridh_iterate = gridh_iterate + 1; % in cm, now compare to gridh(1,1,1) and the other areas, so if its over the height of the cell switch no2 concn
        end
        no2_iterate(2) = (no2_iterate(2) + no2_layer)*1; % Now units of g no2/cm2
    end
    no2_iterate = no2_iterate;
end

我怀疑我的问题在第二个 if 语句中,我在其中指定我希望范围在两个单独的变量之间,我以某种方式排除了所有变量。

【问题讨论】:

  • for h = 1; 的目的是什么? no2_iterate = no2_iterate 也毫无意义。如果你能解释你试图实现的方程式,它可能更容易提供帮助。我看不到您要计算的内容。还请包括gridh(lato,lono,:)no2_moleccm3(lato,lono,:,12); 的内容,这些值用于计算,需要定义以便我复制粘贴和运行您的代码。见minimal reproducible example

标签: matlab loops for-loop if-statement


【解决方案1】:

我最终找出了问题所在。这是解决我麻烦的代码!

还要感谢评论员帮助我清除了我的代码中没有做任何实际操作的一些噪音。

no2_sum_1cm = 0;
gridh_iterate = 0 % starting height in cm
lato = 1;
lono = 1;
no2_iterate_start = 0;
no2_iterate(1:27) = 0;

if gridh_iterate <= gridh(lato,lono,1);
    no2_layer = no2_moleccm3(lato,lono,1,12);
    for i = 1:gridh(lato,lono,1);
        gridh_iterate = gridh_iterate+ 1; % in cm, now compare to gridh(1,1,1) and the other areas, so if its over the height of the cell switch no2 concn
        no2_iterate(1) = (no2_iterate(1) + no2_layer)*1; % Now units of g no2/cm2
    end
end

gridh_iterate = 0;
if gridh_iterate <= gridh(lato,lono,2) %&& gridh_iterate>gridh(lato,lono,1);
    no2_layer = no2_moleccm3(lato,lono,2,12);
    for i = 1:gridh(lato,lono,2);
        gridh_iterate = gridh_iterate + 1; % in cm, now compare to gridh(1,1,1) and the other areas, so if its over the height of the cell switch no2 concn
        no2_iterate(2) = (no2_iterate(2) + no2_layer)*1; % Now units of g no2/cm2
    end
end

我最终意识到,由于“gridh”变量指定了单个单元格的高度而不是总高度,因此列出要保留的高度数据范围是无关紧要的,而且出于我的目的,我应该只设置一个单元格的高度我感兴趣的网格单元格是最大值,这样我的代码就会遍历它然后停止。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-02
    • 2011-09-12
    • 2018-05-27
    • 2019-03-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多