A question about ismember function
1 Ansicht (letzte 30 Tage)
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 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
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,:)?
Dave B am 21 Okt. 2021
Bearbeitet: Dave B am 21 Okt. 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([1 3 5],:)
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
tbl.Word(tbl.Count>20)' % Words that occurred more than 20 times
% mean of smokers, non smokers, and smokers with weight above the median
[mean(Diastolic(Smoker)) mean(Diastolic(~Smoker)) mean(Diastolic(Smoker & Weight>median(Weight)))]