MATLAB: Matching and adding data from multiple columns between different arrays

allindexinglogical indexingmatching

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!

Best Answer

[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)]