Merging two matrices of different dimensions in Matlab?

I have two matrices Aand Bin Matlab. A has a dimension mx6, for example

A=[  1     1     1     1   |  1     0;
     1     1     1     2   |  1     0;
     1     1     1     3   |  1     0;
     1     1     1     4   |  1     0;
     1     2     3     2   |  1     0;
     1     2     3     3   |  1     0;
     1     2     3     4   |  1     0]

Bhas a dimension nx6for example

B=[  1     1     1     1   |  1     1;
     1     2     3     1   |  1     1]

I want to combine Aand Bcreate a matrix Cafter these steps without using loops:

1) Consider B(i,1:4); if there jis one that A(j,1:4)is equal to B(i,1:4)[this can happen no more than one j], and then C(i,:)=[B(i,1:4) A(j,5)+B(i,5) A(j,6)+B(i,6)]. Do it for everyone i=1,...,n.

2) Fill the remaining lines Cwith lines Aand B, which cannot be matched in accordance with step 1).

In the example

C=[  1     1     1     1   |  2     1;  %Step 1) above
   ------------------------------------
     1     1     1     2   |  1     0;  %Step 2) above
     1     1     1     3   |  1     0;  %firstly rows from A
     1     1     1     4   |  1     0;
     1     2     3     2   |  1     0;
     1     2     3     3   |  1     0;
     1     2     3     4   |  1     0; 
     1     2     3     1   |  1     1]  %lastly rows from B

My attempt using loops:

%STEP 1
     for i=1:size(B,1)
            for j=1:size(A,1)
                if all(B(i,1:4)==A(j,1:4),2)
                   C(i,:)=[B(i,1:4) A(j,5)+B(i,5) A(j,6)+B(i,6)]
                end
            end
        end



 %STEP 2


  C=[ C; A(logical(1-ismember(A(:,1:4), B(:,1:4),'rows')),:)];
  C=[ C; B(logical(1-ismember(B(:,1:4), A(:,1:4),'rows')),:)]; 
+4
source share
2

, unique accumarray. unique , , . , A B.

A B , unique . unique , , , unique, , .

'rows' 'stable', , , 'stable', , () (). 'stable', , , .

, 'stable'. , accumarray . accumarray , , ID/ - , . , , - . accumarray , , - . accumarray sum , , .

accumarray , , , , unique. , unique, accumarray , , , .

- :

%// Your data
A=[  1     1     1     1     1     0;
1     1     1     2     1     0;
1     1     1     3     1     0;
1     1     1     4     1     0;
1     2     3     2     1     0;
1     2     3     3     1     0;
1     2     3     4     1     0];
B=[  1     1     1     1     1     1;
1     2     3     1     1     1];

%// Solution
[cols,~,id] = unique([A(:,1:4); B(:,1:4)], 'rows', 'stable');
out = accumarray(id, [A(:,5); B(:,5)]);
out2 = accumarray(id, [A(:,6); B(:,6)]);
final = [cols out out2];

:

final =

     1     1     1     1     2     1
     1     1     1     2     1     0
     1     1     1     3     1     0
     1     1     1     4     1     0
     1     2     3     2     1     0
     1     2     3     3     1     0
     1     2     3     4     1     0
     1     2     3     1     1     1
+2

. , , , .

ind = 1:size(B,1);

[indA, indA_true] = ismember(A(:,1:4),B(:,1:4),'rows');

indB = indA_true(find(indA_true));

C = [A(indA,1:4), A(indA,5:6) + B((indB),5:6)];

C = [C; A(~indA,:); B(~ismember(ind',indB,'rows'),:)];
0

All Articles