Matlab矩阵划分

发布于 2024-12-15 23:57:30 字数 85 浏览 6 评论 0原文

我想按近似均匀的行数来划分矩阵。例如,如果我有一个尺寸为 155 x 1000 的矩阵,如何将其除以 10,其中每个新矩阵的尺寸近似为 15 X 1000?

I would like to partition a matrix by an approximate even amount of rows. For example, if I have a matrix by these dimensions 155 x 1000, how can I partition it by 10 where each new matrix has the approximate dimensions 15 X 1000?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

围归者 2024-12-22 23:57:30

这个怎么样:

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]

How about this:

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]
不必你懂 2024-12-22 23:57:30

这是你想要的吗?

%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),但这有点难以阅读。

Is this what you want?

%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

The only trick here is the use of the end keyword within a function evaluation in the subscripting. Without using the keyword you could use size(x,1), but that is a bit harder to read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文