# Locate an element in an array

2 views (last 30 days)
Mario Martos on 22 May 2016
Commented: Guillaume on 22 May 2016
Hello, I wanted to ask about how to locate the items that I have in a matrix or vector in a larger matrix and remove the row in which it is located. For example I have the following matrix (in my case is bigger than this) :
a =
3.4000 5.5000 8.6000
2.4000 6.3000 7.7000
7.7000 2.3000 6.9000
8.8000 5.7000 9.9000
And another where the elements that interest me in to find are :
b =
6.3000 1.0000
5.7000 2.0000
And what I want and I want is to find the elements of the first column of b in the second column of a; and once found remove the entire row where these elements are b , resulting in :
res =
2.4000 6.3000 7.7000
8.8000 5.7000 9.9000
6.3000 and 5.7000 which are the elements of b found in a with its corresponding row. Thanks in advanced.

Andrei Bobrov on 22 May 2016
Edited: Andrei Bobrov on 22 May 2016
res = a(ismember(a(:,2),b(:,1)),:);
Andrei Bobrov on 22 May 2016
Yes, corrected.
Mario Martos on 22 May 2016
Thank you very much , why not just catch me some elements of the array instead of all ... because there are more of the same but only identifies me and take a few

Guillaume on 22 May 2016
From your example. it looks like you want to keep the rows where the elements are found, not remove. Anyway, to find which set of numbers belong to another set you use ismember:
%find which rows of the 2nd column of a belong to the first column of b, and keep these:
res = a(ismember(a(:, 2), b(:, 1)), :)
%and if you want to remove the rows, you simply invert the result of ismember:
%res = a(~ismember(a(:, 2), b(:, 1)), :)
Mario Martos on 22 May 2016
Thank you very much , why not just catch me some elements of the array instead of all ... because there are more of the same but only identifies me and take a few
Guillaume on 22 May 2016
I don't understand your sentence at all. If the answer is not what you want (it is according to what you've asked so far), then provide an example where the solution does not work and the result you would expect instead.