Reputation: 935
I have two matrices:
$X$ =
1 2 3
4 5 6
7 8 9
$Y$ =
1 10 11
4 12 13
7 14 15
I know that if I want to find the index of a specific element in $X$ or $Y$, I can use the function ''find''. For example: $index_3 = find(X==3)$.
1) My first question is how can i find (or test) if $X$ contains some elements that are also present in $Y$? can I use the function ''find''? if yes so how?
2) The second question is related to the first. Now I want to find (or test) if some columns in $X$ are also present in $Y$. For example, how can I demonstrate in matlab that the column $[1;2;3]$ in $X$ is also present in $Y$?
Any help will be very appreciated.
Upvotes: 0
Views: 45
Reputation: 16810
You can do both of these with ismember
. For the first one, it's simply:
locsX = ismember(X, Y);
For your test matrices this gives you:
locsX =
1 0 0
1 0 0
1 0 0
For another example:
X =
1 2 3
4 5 6
7 8 9
Z =
1 7 13
3 9 15
5 11 17
>> ismember(X, Z)
ans =
1 0 1
0 1 0
1 0 1
For the second part of your question, ismember
has an optional flag to compare rows:
rowsX = ismember(X, Y, 'rows');
so to get the columns, just take the transpose of both matrices:
rowsX = ismember(X.', Y.', 'rows')
rowsX =
1
0
0
Upvotes: 1