【问题标题】:How to Concatenate multiples variables with different size using Matlab ?如何使用 Matlab 连接多个不同大小的变量?
【发布时间】:2016-02-23 20:24:12
【问题描述】:

我有多个不同大小的变量:A1xB1 , A2xB2, A3xB3, ...

我想将它们全部放在一个 VOLUME 中,例如 AxBxC

假设I 可以采用大小为AixBi 的值。

我发现下面的循环:

for ... 
     Volume = cat(3,Volume,I)
endfor

可以连接 I 并在 I 大小相同的情况下产生 VOLUME

但是当I 可以采用不同的尺寸时,我该怎么办?

【问题讨论】:

  • 这没有意义; AxBA1xB1A2xB2等是什么关系?
  • 正如@OliverCharlesworth 所指出的,您只能连接相同大小的数组,因为结果必须具有明确定义的大小n1 x n2 x n3。您可以将原始变量用零填充到出现的最大尺寸,您可以使用元胞数组,每个元素都是一个不同大小的二维数组。
  • 好吧,为了更清楚,假设 A1=A2=A 等和 B1=B2=B 我可以有一个包含大小为 AxB 的 C 个元素的卷
  • @AndrasDeak 谢谢,这就是我想要的。我会尝试使用 Cell Arrays。填充不适合我的情况。
  • 谢谢:) 我编辑了我原来的答案,我意识到使用deal 是不必要的。但是,如果您想反转串联,它会有用处,所以我想我会让您知道这一点。

标签: matlab loops concatenation


【解决方案1】:

您只能使用cat 连接大小相同的数组,因为生成的数组必须是大小为n1 x n2 x n3 的正确数组。由于在 cmets 中您告诉我们填充变量不是一种选择,因此您必须使用元胞数组,其中每个元素将对应于您的一个矩阵。

你可以使用循环,

C = cell(1,nmats); %nmats number of arrays to concatenate
for n=1:nmats
   C{n} = ...; %your n-th array goes here
end

或者对于预定义的个数组,您也可以调用

%C = cell(1,nmats);
%[C{:}] = deal(arr_1,arr_2,...*add variables here*...,arr_nmats);
C = {arr_1,arr_2,...*add variables here*...,arr_nmats};

我注释掉了我的原始版本,它可以工作,但不必要地复杂。但是,deal 的方法将有助于反转您的串联:

[arr_1,arr_2,...*add variables here*...,arr_nmats] = deal(C{:});

同样的效果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    相关资源
    最近更新 更多