Ignore NaN value when iterating

조회 수: 6 (최근 30일)
Jonathan Cheong
Jonathan Cheong 2021년 3월 21일
편집: Ive J 2021년 3월 21일
Hello, say for example I would like to iterate each row, and ignore any NaN values in a matrix.
For example a 3x5 matrix (A):
[1 2 3 4 5 ]
[1,NaN,NaN,NaN,NaN]
[1 2 3 NaN,NaN]
I want the length of each row without NaN for iteration.
Hence the result will be:
[5]
[1]
[3]
for i = 1:size(A,1)
% I want the length of each row without NaN values
nonan = length(A(i,isnan(A)==0))
% Then use that for another for loop...
for 1:length(nonan)
...
end
end
How can I code this? Thanks in advance.

채택된 답변

Ive J
Ive J 2021년 3월 21일
편집: Ive J 2021년 3월 21일
arrayfun(@(x)numel(A(x, ~isnan(A(x, :)))), 1:size(A,1))
5 1 3

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by