How to select rows in a matrix based on the values in another matrix?

10 ビュー (過去 30 日間)
Lu Da Silva
Lu Da Silva 2022 年 4 月 27 日
回答済み: Jonas 2022 年 4 月 27 日
Hi,
I have a matrix A and a matrix B:
-------- A ------- ---------- B ---------
201918 4.5 201908 7 2.1
201917 4.1 201918 8 0.3
201821 -7.1 201917 9 0.1
201718 0.2 201916 9 2
201903 6 18.1
201718 0 1.9
I need to intersect their first columns in order to find matching values within B, thus obtaining a matrix C:
---------- C --------
201918 8 0.3
201917 9 0.1
201718 0 1.9
By using the function intersect(A(:,1), B(:,1)) I get the first column of C but how do I get the corresponding values of the other rows in B?

採用された回答

David Hill
David Hill 2022 年 4 月 27 日
C=B(ismember(B(:,1),A(:,1)),:);

その他の回答 (1 件)

Jonas
Jonas 2022 年 4 月 27 日
use
[~,ia,ib])=intersect(A(:,1), B(:,1));
A(ia,:)
B(ib,:)

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by