Matching and adding data from multiple columns between different arrays

9 ビュー (過去 30 日間)
Si Cla
Si Cla 2018 年 2 月 28 日
コメント済み: Si Cla 2018 年 3 月 5 日
Hello,
I have two matrixes of nx3 (A) and a larger nx7 (B) where B also has more rows. All of the data in A matches data in B, but B also has additional data which I do not need. I want to find where these match i.e. column (A(:,1) == B(:,1) and (A(:,2) == B(:,2) and (A(:,3) == B(:,3). Where these rows match I want to append the data in the extra columns in B (:,4:7) to A. The end result should be a nx7 matrix.
My problem comes from matching three columns in A to data in three columns in B. Trying something like:
I = all(A(:,1)== B(:,1))& all(A(:,2)==B(:,2))& all(A(:,3)==B(:,3));
doesn't work as I get the error "dimensions must agree" - I get the same issue when trying to use intersect, ismember, and setdiff also. I thought I could get around this using logical indexing but so far it hasn't worked.
Is there a way around this?
Many thanks!

採用された回答

Guillaume
Guillaume 2018 年 2 月 28 日
[isinB, where] = ismember(A, B(:, 1:3), 'rows');
assert(all(isinB), 'some rows of A are not in B');
A = [A, B(where, 4:end)]
  1 件のコメント
Si Cla
Si Cla 2018 年 3 月 5 日
Excellent! Much better than my attempt. Thankyou.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by