【问题标题】:MATLAB Merging ArraysMATLAB 合并数组
【发布时间】:2015-06-20 09:30:21
【问题描述】:

我无法弄清楚如何合并两个数组。我的数据是这样的,数组 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;]

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

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

谢谢,

【问题讨论】:

  • 合并标准是什么?
  • 您只是对行进行排序吗?
  • 我只是对行进行排序以获得上面的最终合并数组(“C”)。谢谢,

标签: arrays matlab merge reshape


【解决方案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

使用

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

【讨论】:

  • 对不起,在试图简化问题时,我已经简化了太多。让我在示例中使用更准确的数据提出问题。
  • 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 我认为除非您用文字解释合并标准,否则回答者的猜测太多了。
  • 当然。我试图在数组 A 的前两行之后插入数组 B 的前三行,然后通过在数组 A 的第 4 行之后插入数组 B 的最后三行来重复此操作,以实现一个合并数组“C”。希望这可以澄清。我的数据更大更复杂,但我认为这应该足够了,我可以调整你的答案。
  • @user2100039 你能把这些信息编辑成你的问题吗?
【解决方案2】:

方法#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,:)

【讨论】:

  • 这是我想不到的矢量化方法+1
【解决方案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)

reshape 中的[] 表示“您需要多少才能达到元素总数”。因此,如果我们在 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

【讨论】:

  • hi - reshape 参数中的“2”和 reshape 参数中的 [ ] 右括号是什么意思?
  • @user2100039 我修改了代码来解释神奇的2 值......它是列数。我将添加关于[] 部分的注释。
  • 避免permute的好方法!
  • @Divakar 谢谢! permute 让我头疼 ;)
  • 谢谢 - 你有一个很棒的解决方案 - 将尝试在未来的应用程序中使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
相关资源
最近更新 更多