Star
Star

Reputation: 2299

Combining columns of two matrices of different dimension in Matlab?

I have a matrix in Matlab A of dimension nx3, e.g. n=8

   A=[ 0.3 2  2; 
       0.3 7  7; 
       0.3 10 10; 
       0   15 15; 
       0.3 18 2; 
       0.3 23 7; 
       0   26 10;  
       0.3 31 15]

and a matrix B of dimension mx4, e.g. m=17

B=[1  1  0.05  0.05;
   2  2  0.22  0.22;
   3  3  0.19  0.05;
   5  5  0.02  0.02;
   6  6  0.19  0   ;
   7  7  0.30  0.11;
   10 10 0.27  0.08;
   11 11 0.19  0   ;
   12 12 0.05  0.05;
   18 2  0.25  0.08;
   19 3  0.25  0.08;
   21 5  0.02  0.02;
   22 6  0.22  0.08;
   23 7  0.22  0.08;
   30 14 0.19  0.08;
   31 15 0.19  0.08;
   32 16 0.05  0.05]

I want to create a matrix C following these steps WITHOUT USING LOOPS:

1) Generate C=[];

2) Consider B(i,1). If there exists A(j,2)=B(i,1) [it can happen only once] report C=[ C; B(i,1) B(i,2) B(i,3) B(i,4) A(i,1)]. Do this for i=1,...,m.

3) Consider B(h,1) such that there is no j with A(j,2)=B(h,1). Report C=[C; B(h,1) B(h,2) B(h,3) B(h,4) 0]. Do this for h=1,...,m.

4) Consider A(h,2) such that there is no j with B(j,1)=A(h,2). Report C=[C; A(h,2) A(h,3) 0 0 A(h,1)]. Do this for h=1,...,n.

In the example above I want to get

C=[2  2  0.22  0.22  0.3;
   7  7  0.30  0.11  0.3;
   10 10 0.27  0.08  0.3;
   18 2  0.25  0.08  0.3;
   23 7  0.22  0.08  0.3;
   31 15 0.19  0.08  0.3; %end step 2)
   ---------------------
   1  1  0.05  0.05  0  ;
   3  3  0.19  0.05  0  ;
   5  5  0.02  0.02  0  ;
   6  6  0.19  0     0  ;
   11 11 0.19  0     0  ;
   12 12 0.05  0.05  0  ;
   19 3  0.25  0.08  0  ;
   21 5  0.02  0.02  0  ;
   22 6  0.22  0.08  0  ;
   30 14 0.19  0.08  0  ;
   32 16 0.05  0.05  0  ;
  ----------------------- %end step 3) 
   15 15 0     0     0  ;
   26 10 0     0     0  ]  %end step 4)

These code does what I want but it is too slow with bigger matrices

 C=[];

    %Step 1)
    for l=1:size(B,1)
        for h=1:size(A,1)
            if B(l,1)==A(h,2)
               C=[C; B(l,:) A(h,1)];
            end
        end
    end
    % Steps 2) and 3)
    C=[C; ...
    [B(logical(1-ismember(B(:,1), A(:,2))),:) zeros(size(B(logical(1-ismember(B(:,1), A(:,2))),:),1),1)];...
    [A(logical(1-ismember(A(:,2), B(:,1))),2:3) ...
             zeros(size(A(logical(1-ismember(A(:,2), B(:,1)))),1),2) ...
                      A(logical(1-ismember(A(:,2), B(:,1))),1)]];

Upvotes: 1

Views: 87

Answers (1)

Nemesis
Nemesis

Reputation: 2334

Although it smells strongly like homework, here is some code. See it as a tutorial on matrix operations (tested with Octave).

% Step 1
[~,j,k] = intersect(B(:,1),A(:,2));
C = [B(j,:) A(k,1)];

% Step 2
[~,k] = setdiff(B(:,1),A(:,2));
C = [C; B(k,:) zeros(size(k,1),1)]

% Step 3
[~,k] = setdiff(A(:,2),B(:,1));
C = [C; A(k,[2 3]) zeros(size(k,1),2) A(k,1)]

C =

    2.00000    2.00000    0.22000    0.22000    0.30000
    7.00000    7.00000    0.30000    0.11000    0.30000
   10.00000   10.00000    0.27000    0.08000    0.30000
   18.00000    2.00000    0.25000    0.08000    0.30000
   23.00000    7.00000    0.22000    0.08000    0.30000
   31.00000   15.00000    0.19000    0.08000    0.30000
    1.00000    1.00000    0.05000    0.05000    0.00000
    3.00000    3.00000    0.19000    0.05000    0.00000
    5.00000    5.00000    0.02000    0.02000    0.00000
    6.00000    6.00000    0.19000    0.00000    0.00000
   11.00000   11.00000    0.19000    0.00000    0.00000
   12.00000   12.00000    0.05000    0.05000    0.00000
   19.00000    3.00000    0.25000    0.08000    0.00000
   21.00000    5.00000    0.02000    0.02000    0.00000
   22.00000    6.00000    0.22000    0.08000    0.00000
   30.00000   14.00000    0.19000    0.08000    0.00000
   32.00000   16.00000    0.05000    0.05000    0.00000
   15.00000   15.00000    0.00000    0.00000    0.00000
   26.00000   10.00000    0.00000    0.00000    0.00000

Upvotes: 1

Related Questions