フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How to turn values in one matrix corresponding to a specific element in another into NaN

3 ビュー (過去 30 日間)
Alexander Engman
Alexander Engman 2018 年 4 月 19 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日

Hi!

If I have two matrices:

A=[1 2 NaN; 4 NaN 6; NaN 8 9]
B=[11 12 13; 14 15 16; 17 18 19]

And I want to turn the elements in B corresponding to the elements that are NaN in A also into NaN, how would I do it? Basically the end result I want is:

A=[1 2 NaN; 4 NaN 6; NaN 8 9]
B=[11 12 NaN; 14 NaN 16; 17 NaN 19]

But my matrices are a lot bigger and I need a systematic approach.

Thanks in advance!

回答 (1 件)

Bob Thompson
Bob Thompson 2018 年 4 月 19 日
nanarray = isnan(A);
B(nanarray==1) = NaN;

Did you mean to have the B(3,2)=NaN instead of B(3,1)?

  4 件のコメント
Guillaume
Guillaume 2018 年 4 月 19 日
Well, the thing is that nanarray==1 returns the exact same logical array you started with.
Bob Thompson
Bob Thompson 2018 年 4 月 19 日
Yes, and thinking about it in greater detail I can see how. The challenge for me when indexing in matlab is that I am a very visual person, and a lot of times with the logic indexing I don't take the time to look at every in between step, so I don't automatically know exactly what is happening. Therefore, in favor of being safe I tend to unintentionally build redundancies into my code to make sure it works. It's a dangerous habit, but one I simply haven't been coding long enough to break.

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by