如何将矩阵划分为大小不等的子矩阵?

发布于 2024-10-09 23:35:16 字数 321 浏览 6 评论 0原文

我想知道是否可以使用 mat2cell 函数将 MxN 矩阵划分为 10 个具有相同列大小 N 的子矩阵,并且大约相同的行大小~M/10?如果 mod(M, 10) == 0 则所有子矩阵将具有相同的大小,否则一些矩阵将具有 +/-1 行。这可以通过 mat2cell 函数实现吗?

作为参考,如果行大小都相同,则相当简单,如下所述:
如何将矩阵分成相等的部分?

I am wondering if it is possible to use the mat2cell function to divide an MxN matrix into 10 submatrices with the same column size, N, and approximately the same row size ~M/10? If mod(M, 10) == 0 then all submatrices will have the same size, otherwise a few matrices will have +/-1 row. Is this possible via the mat2cell function?

For reference, if the row sizes are all the same it's fairly straightforward, as explained here:

How to divide a matrix into equals parts?

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

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

发布评论

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

评论(2

酒儿 2024-10-16 23:35:16

这是使用函数 linspacerounddiff

[M, N] = size(mat);  % Matrix size
nSub = 10;           % Number of submatrices
cMat = mat2cell(mat, diff(round(linspace(0, M, nSub+1))), N);

此方法将在单元阵列的结果单元的更统一的方式。请注意使用 mat = magic(5);(左)和 mat = magic(13);(右)应用上述内容时将获得的输出:

cMat =              cMat = 

    [1x5 double]        [1x13 double]
    [0x5 double]        [2x13 double]
    [1x5 double]        [1x13 double]
    [0x5 double]        [1x13 double]
    [1x5 double]        [2x13 double]
    [0x5 double]        [1x13 double]
    [1x5 double]        [1x13 double]
    [0x5 double]        [1x13 double]
    [1x5 double]        [2x13 double]
    [0x5 double]        [1x13 double]

如果您想要随机分布额外的行,您可以使用 randperm< /code>像这样:

subSizes = diff(round(linspace(0, M, nSub+1)));
cMat = mat2cell(mat, subSizes(randperm(nSub)), N);

Here's a simple solution using the functions linspace, round, and diff:

[M, N] = size(mat);  % Matrix size
nSub = 10;           % Number of submatrices
cMat = mat2cell(mat, diff(round(linspace(0, M, nSub+1))), N);

This approach will distribute extra rows in a more uniform fashion across the resulting cells of the cell array. Note these outputs that you will get when applying the above using mat = magic(5); (left) and mat = magic(13); (right):

cMat =              cMat = 

    [1x5 double]        [1x13 double]
    [0x5 double]        [2x13 double]
    [1x5 double]        [1x13 double]
    [0x5 double]        [1x13 double]
    [1x5 double]        [2x13 double]
    [0x5 double]        [1x13 double]
    [1x5 double]        [1x13 double]
    [0x5 double]        [1x13 double]
    [1x5 double]        [2x13 double]
    [0x5 double]        [1x13 double]

If you'd prefer a random distribution of extra rows, you can use randperm like so:

subSizes = diff(round(linspace(0, M, nSub+1)));
cMat = mat2cell(mat, subSizes(randperm(nSub)), N);
多情出卖 2024-10-16 23:35:16

这是可能的,类似于您提供的链接,但您需要决定当 M mod 10 不为 0 时如何划分“剩余”行,以及如果开始的行数少于 10 行,您将做什么。如果列出的假设有效,则以下内容应该有效:

[M,N] = size(X);
Y = mat2cell(X, [repmat(ceil(M/10),[1 mod(M,10)]) ...
                 repmat(floor(M/10),[1 10-mod(M,10)])], N); 

假设:

  1. 您将拥有 >= 10 行(或者您不介意拥有 0xN 数组)
  2. 您满足于将附加行划分在第一个矩阵中 - 即,如果您有13 行,那么您有 3 个 2 行的连续矩阵,后面是 7 个 1 行的矩阵。

例如,我在 X = eye(7) 上运行它并得到:

Y = 
 [1x7 double]
   ...
 [1x7 double]
 [0x7 double]
 [0x7 double]
 [0x7 double]

This is possible and is similar to the link you provided, but you need to decide how you want to divide up the 'leftover' rows when M mod 10 is not 0 and what you will do if there are fewer than 10 rows to begin with. The following should work if the listed assumptions are valid:

[M,N] = size(X);
Y = mat2cell(X, [repmat(ceil(M/10),[1 mod(M,10)]) ...
                 repmat(floor(M/10),[1 10-mod(M,10)])], N); 

Assumptions:

  1. You will have >= 10 rows (or you don't mind having 0xN arrays)
  2. You are content to have the additional rows divided among the first matrices - i.e. if you have 13 rows, then you have 3 consecutive matrices with 2 rows, followed by 7 matrices of 1 row.

For instance, I ran this on X = eye(7) and got:

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