【问题标题】:How to create doubles from cell arrays?如何从单元格数组创建双打?
【发布时间】:2018-04-27 11:11:20
【问题描述】:

请问有没有更高效的代码来完成以下任务:

a = cell(10,1); 

for i = 1 : 10
a{i,1} = randn(200,5);
end

for j =1:5
b{j} = [a{1,1}(:,j) a{2,1}(:,j) a{3,1}(:,j) a{4,1}(:,j) a{5,1}(:,j)];
end

谢谢!

【问题讨论】:

  • 所有单元格都包含相同大小的矩阵吗?
  • 是的,每个单元格都包含一个尺寸为 200x5 的矩阵

标签: matlab cell-array


【解决方案1】:

您的解决方案运行良好。这稍微更紧凑(并且更容易概括)。如果所有单元格都包含相同大小的矩阵,则可以将它们合并到一个矩阵中,然后每 n 选取一列:

for i = 1 : 10
    a{i,1} = randn(200,5);
end

% Transform first five cells in one big matrix
c = cat(2,(a{1:5}));
n = size(a{1} , 2);

b = cell(5,1);
for j =1:5
    % Take one column every 5 (or every "n" in general)
    b{j} = c(: , 1:n:end );
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多