【问题标题】:Matlab matrix partitioningMatlab矩阵划分
【发布时间】:2011-12-29 00:54:44
【问题描述】:

我想按近似偶数行划分矩阵。例如,如果我有一个这些尺寸为 155 x 1000 的矩阵,我如何将它划分为 10,而每个新矩阵的尺寸约为 15 X 1000?

【问题讨论】:

  • “近似偶数”是指有些分区应该有 15 行,有些应该有 16 行,还是您想将每一行随机分配给一个分区(这样由于随机性,一个分区可能有 0 或 20 或更多行)?
  • 扩展 @kristi 的评论,您是否希望分区大小的可变性使它们都相似,或者相等大小的分区加上不同大小的一个,以处理额外的?

标签: matlab matrix partition-problem


【解决方案1】:

这个怎么样:

inMatrix = rand(155, 1000);
numRows  = size(inMatrix, 1);
numParts = 10;

a = floor(numRows/numParts);          % = 15
b = rem(numRows, numParts);           % = 5
partition = ones(1, numParts)*a;      % = [15 15 15 15 15 15 15 15 15 15]
partition(1:b) = partition(1:b)+1;    % = [16 16 16 16 16 15 15 15 15 15]
disp(sum(partition))                  % = 155

% Split matrix rows into partition, storing result in a cell array
outMatrices = mat2cell(inMatrix, partition, 1000)

outMatrices = 
[16x1000 double]
[16x1000 double]
[16x1000 double]
[16x1000 double]
[16x1000 double]
[15x1000 double]
[15x1000 double]
[15x1000 double]
[15x1000 double]
[15x1000 double]

【讨论】:

    【解决方案2】:

    这是你想要的吗?

    %Setup
    x = rand(155,4);  %4 columns prints on my screen, the second dimension can be any size
    n = size(x,1);
    step = round(n/15);
    
    %Now loop through the array, creating partitions
    %    This loop just displays the partition plus a divider
    for ixStart = 1:step:n
        part = x(  ixStart:(min(ixStart+step,end))  ,  :  );
        disp(part);
        disp('---------')
    end
    

    这里唯一的技巧是在下标的函数评估中使用end 关键字。如果不使用关键字,您可以使用size(x,1),但这有点难以阅读。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多