在MATLAB中的if语句中使用多个条件的问题

时间:2019-02-20 22:02:47

标签: matlab loops for-loop if-statement

我遇到一个问题,在第一个for循环中,我在no2_iterate(1)处获得了所需的输出,但是在生成no2_iterate(2)的第二个循环中,我什么都没有得到。 / p>

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

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语句内,在该语句中,我指定我希望范围介于两个单独的变量之间,我以某种方式排除了所有变量。

1 个答案:

答案 0 :(得分:0)

我最终弄清楚了这个问题。这是解决我的麻烦的代码!

还要感谢评论员帮助我清除了我的代码中一些没有实际用处的噪声。

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”变量指定了单个单元格的高度,而不是列出要保留的一系列高度数据无关紧要的总高度,出于我的目的,我应该设置a的高度网格单元格令我很感兴趣,因此我的代码将遍历它,然后停止。