Locate an element in an array
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Mario Martos
il 22 Mag 2016
Commentato: Guillaume
il 22 Mag 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.
0 Commenti
Risposta accettata
Andrei Bobrov
il 22 Mag 2016
Modificato: Andrei Bobrov
il 22 Mag 2016
res = a(ismember(a(:,2),b(:,1)),:);
4 Commenti
Più risposte (1)
Guillaume
il 22 Mag 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)), :)
2 Commenti
Guillaume
il 22 Mag 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.
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!