필터 지우기
필터 지우기

manual script for loop

조회 수: 2 (최근 30일)
Augusto Gabriel da Costa Pereira
편집: Jan 2023년 2월 9일
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));

채택된 답변

Jan
Jan 2023년 2월 9일
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
Augusto Gabriel da Costa Pereira
Amazing loop, thanks for another help!
Jan
Jan 2023년 2월 9일
편집: Jan 2023년 2월 9일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by