MATLAB で、行列の NaN 値を空白として表示するにはどうすればよいですか?

21 views (last 30 days)
MathWorks Support Team
MathWorks Support Team on 21 Jun 2013
次のような行列を作成したとき、
A = rand(3,3);
A(ceil(9*rand(3,1))) = nan
以下のような出力が得られます。
A =
0.5651 0.8702 0.1923
NaN NaN 0.7157
0.0237 0.5195 NaN
この出力に含まれる NaN を次のように空白に変換する方法を教えてください。
A =
0.5651 0.8702 0.1923
0.7157
0.0237 0.5195

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 21 Jun 2013
NaN を空白に置き換えるには、行列を文字列配列に変換した後に NaN を空白と置き換える必要があります。例えば、次のようなコードで行列 A の NaN を空白と置き換えることができます。
B = num2str(A)'; % 数値を文字列に変換
I = strfind(B(:)','NaN'); % NaNs を検索
B([I I+1 I+2]) = ' '; % NaN を空白と置換
A = B' % 結果の表示

More Answers (0)

Categories

Find more on 文字と文字列 in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!