A question about ismember function
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Suppose I have a matrix
M=[1 1 1; 1 1 1; 0 1 0; 5 5 5; 9 1 8; 5 5 5]; D=[1 1 1; 5 5 5; 9 1 8];
I=ismember(M,D,'rows');
I want to assign a number 1 to rows in M that are in D and 0 to rows in M that are not in D then, remove from M all the rows that crossponds to 1.
The answer is
I= [1 1 0 1 1 1]'
so I want to remove row in M that crossponds to 1 so I type
M(I,:)=[];
It gives M=[0 1 0]; which is the required.
My question is this correct for all problems of this kind?
what is the meaning of M(I,:) ? is not I a vector, how can we type M(vector,:) ? I mean I understand the command M(2,:) for example it means get me the second row in M but what is the meaning of M(vector,:)?
0 commentaires
Réponse acceptée
Dave B
le 21 Oct 2021
Modifié(e) : Dave B
le 21 Oct 2021
Check out the documentation page here: https://www.mathworks.com/help/matlab/math/array-indexing.html (Indexing with Logical Values is about halfway down)
There's also a nice article on indexing here: https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html
What this boils down to is you can index by row/column numbers: M(3,:) M([1 3 5],:) or by logicals (M([true;false;true;false;true],:))
M=magic(5)
M(3,:)
M([1 3 5],:)
M([true;false;true;false;true],:)
Note that logical indexing is really really useful!
M(M(:,1)>10,:) % Rows of M where the first column is greater than 10
M(mod(M,2)==0)' % Even values in M
load sonnetsTable.mat
tbl.Word(tbl.Count>20)' % Words that occurred more than 20 times
load patients
% mean of smokers, non smokers, and smokers with weight above the median
[mean(Diastolic(Smoker)) mean(Diastolic(~Smoker)) mean(Diastolic(Smoker & Weight>median(Weight)))]
0 commentaires
Plus de réponses (0)
Voir également
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!