Matching and adding data from multiple columns between different arrays
조회 수: 5 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
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)]
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!