首页 > 解决方案 > 在 MATLAB 的 If 语句中使用多个条件的问题

问题描述

我遇到了一个问题,对于第一个 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 语句中,我在其中指定我希望范围在两个单独的变量之间,我以某种方式排除了所有变量。

标签: matlabloopsfor-loopif-statement

解决方案


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

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

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

我最终意识到,因为“网格”变量指定了单个单元格的高度而不是总高度,所以列出要保持的高度数据范围是无关紧要的,为了我的目的,我应该只设置网格单元格的高度我作为最大值很有趣,因此我的代码将遍历它然后停止。


推荐阅读