How to define values in a vector as NaN ?

2 ビュー (過去 30 日間)
meryem berrada
meryem berrada 2021 年 11 月 26 日
コメント済み: Star Strider 2021 年 11 月 26 日
Hello,
I have a variable A that is 2x3 (x and y values) and variable B that is 2x2 (also x and y values). B actually corresponds to some of the x,y pairs in A. I would like to replace the pairs in A that correspond to those in B by NaN.
A = [1 2 3; 4 5 6];
Ax = A(1,:);
Ay = A(2,:);
B = [2 3; 5 6];
Bx = B(1,:);
By = B(2,:);
Ay(By) = NaN %this is multiplying instead of redefining --> not what I want
Ax(By) = NaN %same here
A = [Ay;Ax]
My goal is to have A = [1 NaN NaN; 4 NaN NaN].
Can anyone help please ?

採用された回答

Star Strider
Star Strider 2021 年 11 月 26 日
Try this (using ismember) —
A = [1 2 3; 4 5 6];
B = [2 3; 5 6];
C = ismember(A,B)
C = 2×3 logical array
0 1 1 0 1 1
A(C) = NaN
A = 2×3
1 NaN NaN 4 NaN NaN
.
  2 件のコメント
meryem berrada
meryem berrada 2021 年 11 月 26 日
That was perfect, thank you so much !
Star Strider
Star Strider 2021 年 11 月 26 日
As always, my pleasure!
.

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

その他の回答 (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