Locate an element in an array

조회 수: 3 (최근 30일)
Mario Martos
Mario Martos 2016년 5월 22일
댓글: Guillaume 2016년 5월 22일
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
Andrei Bobrov 2016년 5월 22일
편집: Andrei Bobrov 2016년 5월 22일
res = a(ismember(a(:,2),b(:,1)),:);
  댓글 수: 4
Andrei Bobrov
Andrei Bobrov 2016년 5월 22일
Yes, corrected.
Mario Martos
Mario Martos 2016년 5월 22일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2016년 5월 22일
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
Mario Martos
Mario Martos 2016년 5월 22일
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
Guillaume 2016년 5월 22일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by