フィルターのクリア

find nan and eliminate it in matrix

5 ビュー (過去 30 日間)
moulay
moulay 2012 年 11 月 18 日
hi!
i wann explain may problem with an example:
% A=[1 2 3;5 4 6;nan nan nan]
to eleminate nan: A=(A(~isnan(A)));
i get this array:A=[1 2 3 5 4 6];
why not A=[1 2 3; 5 4 6]?
i know we could use reshape. is there any other method?
thanx

採用された回答

the cyclist
the cyclist 2012 年 11 月 18 日
編集済み: the cyclist 2012 年 11 月 19 日
The reason that a vector rather than a matrix is returned is that MATLAB doesn't know that your "mask" is going to still be rectangular. For example, if your input had been
A = [1 2; 3 NaN];
then your code wouldn't be able to return a matrix.
As Walter's question suggests, you'll need to decide what to do about partial-NaN rows. Probably one of the following is what you want:
% Remove rows with all NaN:
A(all(isnan(A),2),:) = []
% Remove row with any NaN:
A(any(isnan(A),2),:) = []
You could do the analogous operations on columns by changing the 2's to 1's (and using the semicolon in the first index instead of the second).

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 11 月 18 日
What do you want to have happen if the rows contain unequal number of nans? Do you want to delete the entire row if any member of the row contains a nan? Or only delete the row if it is all nan?

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by