MATLAB 合并数组

MATLAB Merging Arrays

我不知道如何合并两个数组。我的数据是这样的,数组 A 和 B。

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;]

我尝试过使用不同的方法来重塑每个数组并尝试使用双循环,但还没有成功。

在我的实际数据中,它是在数组 A 的 3 行之后插入 9 行数组 B,然后重复 100 次。因此,有 12 个新的合并行(数组 A 中的 3 行和数组 B 中的 9 行)重复 100 次,最终行号 == 1200。数组 A 实际数据有 300 行,实际数组 B 数据有 900 行

谢谢,

根据您的新标准,这就是您想要的。我的解决方案不是最好看的(也许有人能想到一个很好的矢量化方法),但它有效

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

使用

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;]

方法 #1

假设我正确地理解了问题的要求,这可能是一种方法 -

%// 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,:)

这是一个仅使用 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)

reshape中的[]表示"how ever many you need to get to the total number of elements"。因此,如果我们在 B 中有 12 个元素并执行:

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

我们正在将 B 重塑为 3x2xP 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