manual script for loop

2 views (last 30 days)
Augusto Gabriel da Costa Pereira
Edited: Jan on 9 Feb 2023
I made this script just below but it is quite manual, I tried several ways to perform a loop, I was not successful in my action.
does anyone have an idea?
I used the matrix "tNino1_2" attached
MedComb(:,1)=1:1:15;
MedComb(1:1:15,2:1:6)=NaN;
xcol=20:1:24;
[r,c] = find(tNino1_2(:,xcol)==1);
[ru,ia,ic] = unique(r);
out = tNino1_2(ru,:);
out1AF = out1(out1(:,20)==1,[4,20])
MedComb(1,2)=nanmean(out1AF(:,1));
xcol=20:1:24;
[r,c] = find(tNino1_2(:,xcol)==2);
[ru,ia,ic] = unique(r);
out2 = tNino1_2(ru,:);
out2AF = out2(out2(:,20)==2,[4,20])
MedComb(2,2)=nanmean(out2AF(:,1));
xcol=20:1:24;
[r,c] = find(tNino1_2(:,xcol)==1);
[ru,ia,ic] = unique(r);
out = tNino1_2(ru,:);
out1RB = out1(out1(:,21)==1,[4,21])
MedComb(1,3)=nanmean(out1RB(:,1));
xcol=20:1:24;
[r,c] = find(tNino1_2(:,xcol)==2);
[ru,ia,ic] = unique(r);
out = tNino1_2(ru,:);
out2RB = out2(out2(:,21)==2,[4,21])
MedComb(2,3)=nanmean(out2RB(:,1));

Accepted Answer

Jan
Jan on 9 Feb 2023
MedComb(:,1) = 1:15;
MedComb(1:15,2:6) = NaN;
xcol = 20:24;
for i1 = 1:2
for i2 = 0:1
[r, c] = find(tNino1_2(:, xcol) == i1);
ru = unique(r);
out = tNino1_2(ru, :); % A typo? "out" is defined, but "out1" used?!
out = out(out(:, 20 + i2) == i1, [4, 20 + i2])
MedComb(i1, 2 + i2) = nanmean(out(:, 1));
end
end
  2 Comments
Augusto Gabriel da Costa Pereira
Amazing loop, thanks for another help!
Jan
Jan on 9 Feb 2023
Edited: Jan on 9 Feb 2023
You are welcome. As far as I can see, this:
[r, c] = find(tNino1_2(:, xcol) == i1);
ru = unique(r);
can be simplified to:
ru = any(tNino1_2(:, xcol) == i1, 1);

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!