Finding the position of a cell within a Matrix

5 vues (au cours des 30 derniers jours)
Charnkamal Bhogal
Charnkamal Bhogal le 5 Juin 2020
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

Réponse acceptée

KSSV
KSSV le 5 Juin 2020
Modifié(e) : KSSV le 5 Juin 2020
Read about isnan.
idx = isnan(Matrix(:,2)) ;
Matrix(idx,:)
% To get the rows
rows = find(idx)
  1 commentaire
Charnkamal Bhogal
Charnkamal Bhogal le 5 Juin 2020
It works!
Thank you very much Sir!

Connectez-vous pour commenter.

Plus de réponses (2)

Ameer Hamza
Ameer Hamza le 5 Juin 2020
Modifié(e) : Ameer Hamza le 5 Juin 2020
isnan() is used to detect nan. You can write your code without for-loop
k = any(isnan(Matrix(:,2)))
  4 commentaires
Ameer Hamza
Ameer Hamza le 5 Juin 2020
If you want to find the row, then something like this will work
idx = find(isnan(Matrix(:,2)))
Charnkamal Bhogal
Charnkamal Bhogal le 5 Juin 2020
@Ameer Hamza, @madhan ravi, @Jake Bowd
Thanks for your replys

Connectez-vous pour commenter.


Jake Bowd
Jake Bowd le 5 Juin 2020
Hi,
Could you use the following?
m = ; % whatever the matrix is called.
[row, column] = find(m == NaN)
  2 commentaires
Charnkamal Bhogal
Charnkamal Bhogal le 5 Juin 2020
I could not use that, because I have loaded the Matrix from a .mat file
Jake Bowd
Jake Bowd le 5 Juin 2020
Arrhh I see :).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by