Matching and adding data from multiple columns between different arrays

14 vues (au cours des 30 derniers jours)
Si Cla
Si Cla le 28 Fév 2018
Commenté : Si Cla le 5 Mar 2018
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!

Réponse acceptée

Guillaume
Guillaume le 28 Fév 2018
[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)]

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by