Extract first non-NaN of each row without loop

조회 수: 9 (최근 30일)
Dave
Dave 2021년 10월 24일
댓글: Dave 2021년 10월 24일
Hello, from a matrix where each row contains at least one non-NaN, is there away to extract the FIRST non-nan of each row without using a loop?
A=[NaN,444,NaN,NaN,41;
NaN,NaN,NaN,222,NaN;
NaN,NaN,554,554,NaN;
125,747,21,NaN,4;
NaN,NaN,NaN,43,NaN;
NaN,98,NaN,75,NaN;
NaN,22,NaN,NaN,NaN]
From matrix A, I would like to arrive to B
B=[444;222;554;125;43;98;22]
Thanks
  댓글 수: 1
Dave
Dave 2021년 10월 24일
편집: Dave 2021년 10월 24일
I am able to do it one row at a time, for example for the last row
A(7,find(~isnan(A(7,:)), 1, 'first'))
But without a loop I cannot do it for all rows

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

채택된 답변

Walter Roberson
Walter Roberson 2021년 10월 24일
A=[NaN,444,NaN,NaN,41;
NaN,NaN,NaN,222,NaN;
NaN,NaN,554,554,NaN;
125,747,21,NaN,4;
NaN,NaN,NaN,43,NaN;
NaN,98,NaN,75,NaN;
NaN,22,NaN,NaN,NaN]
A = 7×5
NaN 444 NaN NaN 41 NaN NaN NaN 222 NaN NaN NaN 554 554 NaN 125 747 21 NaN 4 NaN NaN NaN 43 NaN NaN 98 NaN 75 NaN NaN 22 NaN NaN NaN
B = A(sub2ind(size(A), (1:size(A,1)).', sum(cumprod(isnan(A),2),2)+1))
B = 7×1
444 222 554 125 43 98 22

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by