substituting value in matrix does not work for some reason

A = [6 9 NaN NaN 8 NaN]; B = [2 5 342 232 1 116];
sizeA = size(A);
for m = [1:1:size(2)];
if A(1,m) == NaN;
B(1,m) = NaN;
end;
end;
I was hoping B would become [2 5 NaN NaN 8 NaN], but it did not change. I would appreciate any help

 採用された回答

Kelly Kearney
Kelly Kearney 2014 年 11 月 18 日
編集済み: Kelly Kearney 2014 年 11 月 18 日

1 投票

You can't use == with NaNs:
>> NaN == NaN
ans =
0
Use isnan instead:
B(isnan(A)) = NaN;

その他の回答 (1 件)

Kevin Claytor
Kevin Claytor 2014 年 11 月 18 日

0 投票

Logical indexing makes this much easier;
B(A==NaN) = NaN;

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

質問済み:

2014 年 11 月 18 日

編集済み:

2014 年 11 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by