필터 지우기
필터 지우기

How do I find the number of occurrences of NaN and the corresponding subscripts in an array

조회 수: 1 (최근 30일)
A = NaN 100 101 102 103 104
201 2 7 3 2 2
202 NaN 8 4 5 6
203 NaN NaN 2 3 5
205 3 4 2 6 4
I have a matrix with the first row and first column being headers. I would like to know the no. occurences and subscripts on NaN.
To find no. of occurences, I did
number_of_nan = sum(sum(isnan(A(2:end,2:end))))
Also,
logical_array = isnan(A(2:end,2:end));
numel(logical_array(logical_array == 1));
Is there a simpler/better way. Also how do I find the subscripts of the NaN elements in the array ?
  댓글 수: 2
José-Luis
José-Luis 2013년 1월 9일
What do you mean by subscripts? The linear indexes or the row and column position? The indices relative to what? To the entire array or to the array without column and headers?
Ms. Mat
Ms. Mat 2013년 1월 9일
편집: Ms. Mat 2013년 1월 9일
I would like to know the row and column position. For the given example matrix A,
202, 100
203, 100
203, 101
or
3, 2
4, 2
4, 3
Thank You

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

채택된 답변

José-Luis
José-Luis 2013년 1월 9일
편집: José-Luis 2013년 1월 9일
Another option:
logical_array = A(2:end,2:end) ~= A(2:end,2:end)
num_NaN = sum(logical_array(:));
idx = find(logical_array); %I am not sure this is what you want, please see my comment to your question
EDIT so that was not what your wanted after all. For that:
To get row and column position, according to the header:
logical_array = A(2:end,2:end) ~= A(2:end,2:end)
idx = find(logical_array);
[dim(1) dim(2)] = size(A);
[ii,jj] = ind2sub(dim-1,idx);
your_position = [A(ii+1,1) A(1,jj+1)'];

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by