Make all elements of given row numbers equal to NaN.

3 ビュー (過去 30 日間)
Meghana Dinesh
Meghana Dinesh 2015 年 10 月 6 日
コメント済み: Thorsten 2015 年 10 月 6 日
Given a matrix, if any element has a non-finite value, I want to make all elements of that row equal to NaN. For example:
Input:
A=[1 2 NaN
4 5 6
7 NaN 9
0 1 2
3 4 5
6 7 8
Inf 0 1
2 3 4
5 6 7
8 NaN 0];
Output:
B=[NaN NaN NaN
4 5 6
NaN NaN NaN
0 1 2
3 4 5
6 7 8
NaN NaN NaN
2 3 4
5 6 7
NaN NaN NaN];
How can this be done?
This is how I know:
idx_NaNinf = find((isnan(A)|isinf(A)) );
[idx_row,idx_col] = ind2sub(size(A),idx_NaNinf);
With all row numbers stored in idx_row and by using a for loop, I can make the elements of that row equal to NaN. But is there any other more efficient method?

採用された回答

per isakson
per isakson 2015 年 10 月 6 日
編集済み: per isakson 2015 年 10 月 6 日
I guess this is more efficient, and I think it's more readable
B = A;
is_nan_row = any( isnan( A ), 2 );
is_inf_row = any( isinf( A ), 2 );
B( (is_nan_row|is_inf_row), : ) = nan;
  2 件のコメント
Meghana Dinesh
Meghana Dinesh 2015 年 10 月 6 日
As an extension, if all columns of a row are zeros, I want to make the entire row NaN. How can I do this?
Input:
[0 0 1
0 0 0
7 8 0
0 0 0
8 9 0
0 0 0
0 0 0];
Output required:
[0 0 1
NaN NaN NaN
7 8 0
NaN NaN NaN
8 9 0
NaN NaN NaN
NaN NaN NaN];
Thorsten
Thorsten 2015 年 10 月 6 日
x(~any(x, 2), :) = nan

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by