如何将矩阵划分为大小不等的子矩阵?
我想知道是否可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是使用函数
linspace
、round
和diff
:此方法将在单元阵列的结果单元的更统一的方式。请注意使用
mat = magic(5);
(左)和mat = magic(13);
(右)应用上述内容时将获得的输出:如果您想要随机分布额外的行,您可以使用
randperm< /code>
像这样:
Here's a simple solution using the functions
linspace
,round
, anddiff
: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) andmat = magic(13);
(right):If you'd prefer a random distribution of extra rows, you can use
randperm
like so:这是可能的,类似于您提供的链接,但您需要决定当 M mod 10 不为 0 时如何划分“剩余”行,以及如果开始的行数少于 10 行,您将做什么。如果列出的假设有效,则以下内容应该有效:
假设:
例如,我在 X = eye(7) 上运行它并得到:
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:
Assumptions:
For instance, I ran this on X = eye(7) and got: