필터 지우기
필터 지우기

Removing rows of a matrix based on rows of another matrix!

조회 수: 6 (최근 30일)
Antonio
Antonio 2018년 1월 17일
답변: Steven Lord 2018년 1월 17일
Imagine I have two matrices with different sizes (let's say 6x2 and 5x2) as follows:
A = [47 10;
29 10;
23 10;
34 10;
12 10;
64 10];
B = [23 20;
12 20;
54 20
47 20;
31 20];
I need to compare A(:,1) with B(:,1) and delete the rows in matrix A whose first-column-element is different from matrix B's first-column-element (so my focus is only on first columns of the matrices). So I should eventually get something like this:
A = [47 10;
12 10;
23 10];
as "47", "12", and "23" are the only first-column-elements in A that also exist in B! I have written this but I get the error "Matrix dimensions must agree."!
TF = A(:,1) ~= B(:,1);
A(TF,:) = [];
Any ideas how I could fix this?

채택된 답변

Birdman
Birdman 2018년 1월 17일
A=A(ismember(A(:,1),B(:,1)),:)

추가 답변 (1개)

Steven Lord
Steven Lord 2018년 1월 17일
Since it doesn't seem like ordering matters [you don't require A(n, 1) to match B(n, 1) as long as it matches some element in B(:, 1)] use the ismember function.

카테고리

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