Deleting rows of a matrix based on values from another matrix

조회 수: 22 (최근 30일)
Hari
Hari 2022년 7월 8일
답변: Johan 2022년 7월 8일
I have a m x 3 matrix A, and a nx1 matrix B. I need to remove those rows of A if either column 1 or 2 of the matrix equals any value in B.

채택된 답변

Johan
Johan 2022년 7월 8일
You can use ismember() to do that:
A = randi(5,5,3)
A = 5×3
5 5 4 2 5 3 2 1 3 4 4 4 3 5 3
B = randi(5,2,1)
B = 2×1
5 1
% Find all the row where col 1 to 2 of A have a value in B
mask = any(ismember(A(:,1:2),B),2)
mask = 5×1 logical array
1 1 1 0 1
A(mask,:) = [] % delete the rows of A according to the mask
A = 1×3
4 4 4

추가 답변 (1개)

Kritarth Sinha
Kritarth Sinha 2022년 7월 8일
편집: Kritarth Sinha 2022년 7월 8일
Hello Hari,
You can simply use nested loops and for each value in column 1 or 2,you can check that is present in matrix B and do it accordingly.
Hope this will help.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by