2015-04-14 164 views
2

我无法弄清楚如何合并两个数组。我的数据是这样的与阵列A和B.MATLAB合并阵列

A = [ 0 0; 0 0; 2 2; 2 2;] 

B = [ 1 1; 1 1; 3 3; 3 3; 4 4; 4 4; 5 5; 5 5;] 

,我需要最终阵列“C”看起来像这样合并后:

C = [ 0 0; 0 0; 1 1; 1 1; 2 2; 2 2; 3 3; 3 3; 4 4; 4 4; 5 5; 5 5;] 

我用不同的方式与试图重塑每个数组,并试图使用双循环,但还没有得到它的工作。

以我的实际数据被插入9行阵列B的以下3行阵列A和然后重复100次。所以,有12点新的合并的行(从阵列A 3行和从阵列乙9行)反复进行最终行号== 1200阵列的实际数据具有300行和实际阵列B数据具有900行

100倍

感谢,

+0

什么是融合标准是什么? – Divakar

+1

你只是对行进行排序? – beaker

+0

我简单地排序行以获得上面的最后一个合并数组(“C”)。谢谢, – user2100039

回答

3

这里只是reshape使用的解决方案:

A = [ 6 6; 3 3; 5 5; 4 4;] 
B = [ 0 0; 21 21; 17 17; 33 33; 29 29; 82 82;] 

A_count = 2; 
B_count = 3; 

w = size(A,2); %// width = number of columns 

Ar = reshape(A,A_count,w,[]); 
Br = reshape(B,B_count,w,[]); 

Cr = [Ar;Br]; 
C = reshape(Cr,[],w) 

在重塑[]的意思是“你需要怎么过许多去元素的总数”。所以,如果我们在B 12元,做:

Br = reshape(B,3,2,[]); 

我们正在重塑B3x2xP 3维矩阵。由于元素的总数是12,P = 2,因为12 = 3x2x2

输出:

A = 

    6 6 
    3 3 
    5 5 
    4 4 

B = 

    0 0 
    21 21 
    17 17 
    33 33 
    29 29 
    82 82 

C = 

    6 6 
    3 3 
    0 0 
    21 21 
    17 17 
    5 5 
    4 4 
    33 33 
    29 29 
    82 82 
+0

嗨 - 重塑参数中的“2”和重塑参数中的[]括号括起来的含义是什么? – user2100039

+0

@ user2100039我修改了代码来解释魔术'2'的值......这是列数。我将添加关于'[]'部分的注释。 – beaker

+0

避免'排列'的好解决方案! – Divakar

0

根据新的标准,这就是你想要的。我的解决方案是不看(马爷有人能想到一个很好的量化方法)最好的,但它使用

A = [ 6 6; 3 3; 5 5; 4 4;] 
B = [ 0 0; 21 21; 17 17; 33 33; 29 29; 82 82;] 

产生

C =[6 6; 3 3; 0 0; 21 21; 17 17; 5 5; 4 4; 33 33; 29 29; 82 82;] 
+0

对不起,试图简化这个问题,我已经简化了太多。让我用这个例子中的更准确的数据来提问这个问题。 – user2100039

+0

A = [6 6; 3 3; 5 5; 4 4;],B = [0 0; 21 21; 17 17; 33 33; 29 29; 82 82;]和最终阵列C = [6 6; 3 3; 0 0; 21 21; 17 17; 5 5; 4 4; 33 33; 29 29; 82 82;]。 – user2100039

+2

@ user2100039我认为,除非你用文字解释合并标准,否则你会在答题器上留下太多的猜测。 – Divakar

3

方法#1的工作

a_step = 2; 
b_step = 3; 

C = zeros(size([A;B])); 

%we use two iterators, one for each matrix, they must be initialized to 1 
a_idx = 1; 
b_idx = 1; 

%this goes through the entire c array doing a_step+b_step rows at a 
%time 
for c_idx=1:a_step+b_step :size(C,1)-1 
    %this takes the specified number of rows from a 
    a_part = A(a_idx:a_idx+a_step-1,:); 

    %tkaes the specified number of rows from b 
    b_part = B(b_idx:b_idx+b_step-1,:); 

    %combines the parts together in the appropriate spot in c 
    C(c_idx:c_idx + a_step + b_step -1,:) = [a_part;b_part]; 

    %advances the "iterator" on the a and b matricies 
    a_idx = a_idx + a_step; 
    b_idx = b_idx + b_step; 
end 

这可能是一种方法assumi NG我的问题,权利的要求 -

%// Inputs 
A = [ 6 6; 3 3; 5 5; 4 4;]; 
B = [ 0 0; 21 21; 17 17; 33 33; 29 29; 82 82;]; 

%// Parameters that decide at what intervals to "cut" A and B along the rows 
A_cutlen = 2; %// Edit this to 3 for the actual data 
B_cutlen = 3; %// Edit this to 9 for the actual data 

%// Cut A and B along the rows at specified intervals into 3D arrays 
A3d = permute(reshape(A,A_cutlen,size(A,1)/A_cutlen,[]),[1 3 2]) 
B3d = permute(reshape(B,B_cutlen,size(B,1)/B_cutlen,[]),[1 3 2]) 

%// Vertically concatenate those 3D arrays to get a 3D array 
%// version of expected output, C 
C3d = [A3d;B3d] 

%// Convert the 3D array to a 2D array which is the final output 
C_out = reshape(permute(C3d,[1 3 2]),size(C3d,1)*size(C3d,3),[]) 

采样运行 -

A = 
    6  6 
    3  3 
    5  5 
    4  4 
B = 
    0  0 
    21 21 
    17 17 
    33 33 
    29 29 
    82 82 
A_cutlen = 
    2 
B_cutlen = 
    3 
C_out = 
    6  6 
    3  3 
    0  0 
    21 21 
    17 17 
    5  5 
    4  4 
    33 33 
    29 29 
    82 82 

方法2

只为bsxfun的爱,这里有一个方法它和ones(无reshapepermute) -

%// Assuming A_cutlen and B_cutlen decide cutting intervals for A and B 
%// Concatenate A and B along rows 
AB = [A;B] 

%// Find the row indices corresponding to rows from A and B to be placed 
%// according to the problem requirements 
idx1 = [1:A_cutlen size(A,1)+[1:B_cutlen]] 
idx2 = [A_cutlen*ones(1,A_cutlen) B_cutlen*ones(1,B_cutlen)] 
idx = bsxfun(@plus,idx1(:),idx2(:)*[0:size(A,1)/A_cutlen-1]) 

%// Re-arrange AB based on "idx" for the desired output 
C = AB(idx,:) 
+0

这是我想不到的矢量化方法+1 – andrew