2015-04-14 168 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