manual script for loop

2 visualizzazioni (ultimi 30 giorni)
Augusto Gabriel da Costa Pereira
Modificato: Jan il 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));

Risposta accettata

Jan
Jan il 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 Commenti
Augusto Gabriel da Costa Pereira
Amazing loop, thanks for another help!
Jan
Jan il 9 Feb 2023
Modificato: Jan il 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);

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by