简体   繁体   English

在Matlab中合并两个不同维度的矩阵的列?

[英]Combining columns of two matrices of different dimension in Matlab?

I have a matrix in Matlab A of dimension nx3 , eg n=8 我在Matlab A中有一个尺寸为nx3的矩阵,例如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 , eg m=17 和尺寸为mx4的矩阵B ,例如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: 我想按照以下步骤创建矩阵C无需使用环:

1) Generate C=[] ; 1)产生C=[] ;

2) Consider B(i,1) . 2)考虑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)] 如果存在A(j,2)=B(i,1) [它只能发生一次]报告C=[ C; B(i,1) B(i,2) B(i,3) B(i,4) A(i,1)] C=[ C; B(i,1) B(i,2) B(i,3) B(i,4) A(i,1)] . C=[ C; B(i,1) B(i,2) B(i,3) B(i,4) A(i,1)] Do this for i=1,...,m . i=1,...,m执行此操作。

3) Consider B(h,1) such that there is no j with A(j,2)=B(h,1) . 3)考虑B(h,1)使得没有jA(j,2)=B(h,1) Report C=[C; B(h,1) B(h,2) B(h,3) B(h,4) 0] 报告C=[C; B(h,1) B(h,2) B(h,3) B(h,4) 0] C=[C; B(h,1) B(h,2) B(h,3) B(h,4) 0] . C=[C; B(h,1) B(h,2) B(h,3) B(h,4) 0] Do this for h=1,...,m . h=1,...,m执行此操作。

4) Consider A(h,2) such that there is no j with B(j,1)=A(h,2) . 4)考虑A(h,2)使得不存在j( B(j,1)=A(h,2) Report C=[C; A(h,2) A(h,3) 0 0 A(h,1)] 报告C=[C; A(h,2) A(h,3) 0 0 A(h,1)] C=[C; A(h,2) A(h,3) 0 0 A(h,1)] . C=[C; A(h,2) A(h,3) 0 0 A(h,1)] Do this for h=1,...,n . 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)]];

Although it smells strongly like homework, here is some code. 尽管它闻起来很像作业,但这里有一些代码。 See it as a tutorial on matrix operations (tested with Octave). 将其视为有关矩阵运算的教程(已通过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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM