Finding the position of a cell within a Matrix

조회 수: 2 (최근 30일)
captainjoker
captainjoker 2020년 6월 5일
댓글: captainjoker 2020년 6월 5일
Hello Fellow Developer,
i have been given a 100x13 Matrix with Integers in it. But in one cell there is NaN written in it.
I know that the cell with NaN is in column two, so I tried the following code: But my Variable k never changes to one.
for i=1:100
if Matrix(i, 2) == 'NaN'
k = 1
end
end

채택된 답변

KSSV
KSSV 2020년 6월 5일
편집: KSSV 2020년 6월 5일
Read about isnan.
idx = isnan(Matrix(:,2)) ;
Matrix(idx,:)
% To get the rows
rows = find(idx)

추가 답변 (2개)

Ameer Hamza
Ameer Hamza 2020년 6월 5일
편집: Ameer Hamza 2020년 6월 5일
isnan() is used to detect nan. You can write your code without for-loop
k = any(isnan(Matrix(:,2)))
  댓글 수: 4
Ameer Hamza
Ameer Hamza 2020년 6월 5일
If you want to find the row, then something like this will work
idx = find(isnan(Matrix(:,2)))
captainjoker
captainjoker 2020년 6월 5일
@Ameer Hamza, @madhan ravi, @Jake Bowd
Thanks for your replys

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


Jake Bowd
Jake Bowd 2020년 6월 5일
Hi,
Could you use the following?
m = ; % whatever the matrix is called.
[row, column] = find(m == NaN)
  댓글 수: 2
captainjoker
captainjoker 2020년 6월 5일
I could not use that, because I have loaded the Matrix from a .mat file
Jake Bowd
Jake Bowd 2020년 6월 5일
Arrhh I see :).

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by