Taylor
Taylor

Reputation: 89

Issue Using Multiple Criteria in If-Statements in MATLAB

I'm having an issue where, for the first for loop I'm getting my desired output at no2_iterate(1) but within the second loop made to generate no2_iterate(2) I'm not getting any output whatsoever.

The following are my two if-statements/for loops to generate 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

I suspect that my issue is within the second if-statement where I specify that I want the range to be between two separate variables that I am somehow excluding all variables.

Upvotes: 0

Views: 36

Answers (1)

Taylor
Taylor

Reputation: 89

I ended up figuring out the problem. Here is the code that solved my troubles!

Also thank you to commentators for helping me clear some of the noise from my code that didn't do anything practical.

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

I ended up realizing since "gridh" variables specified the height of a single cell and not total heights that it was irrelevant to list a range of height data to stay between and that for my purposes I should just set the height of a grid cell I was interesting in as the max, so that my code would iterate through it and then cease.

Upvotes: 0

Related Questions