How to define values in a vector as NaN ?

2 vues (au cours des 30 derniers jours)
meryem berrada
meryem berrada le 26 Nov 2021
Commenté : Star Strider le 26 Nov 2021
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 ?

Réponse acceptée

Star Strider
Star Strider le 26 Nov 2021
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 commentaires
meryem berrada
meryem berrada le 26 Nov 2021
That was perfect, thank you so much !
Star Strider
Star Strider le 26 Nov 2021
As always, my pleasure!
.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by