필터 지우기
필터 지우기

find rows of two matricies (of different sizes) based on matching element of one column

조회 수: 2 (최근 30일)
Given two matrices A[1000x4] and B[500x4] what is the fastest way in MATLAB to extract the complete rows of A which share the same element values in column 4 as B does?
for example if,
A=[1 2 3 7;
2 6 9 1;
8 3 4 8] and
B=[9 2 5 1;
6 2 1 5;
3 6 6 8].
What is the most efficient way to create a new array
C=[2 6 9 1;
8 3 4 8],
having the rows of A with same element in column 4 as B does?
Thanks!

채택된 답변

Image Analyst
Image Analyst 2015년 11월 10일
Try ismember():
A=[1 2 3 7;
2 6 9 1;
8 3 4 8]
B=[9 2 5 1;
6 2 1 5;
3 6 6 8]
[ia, ib] = ismember(A(:,4), B(:, 4))
C = A(find(ib),:)

추가 답변 (1개)

James Tursa
James Tursa 2015년 11월 10일
Caution, untested:
k = ismember(A(:,4),B(:,4));
C = A(k,:);

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by