MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何在MATLAB中调整矩阵的大小? (https://www.labfans.com/bbs/showthread.php?t=22652)

poster 2019-12-10 16:49

如何在MATLAB中调整矩阵的大小?
 
假设我有一个[B]1×12[/B]矩阵,我想将其大小调整为[B]4×3[/B]矩阵。我该怎么办?

我当前的解决方案有点丑陋:

for n = 1:(length(mat)/3) out(n,1:3) = mat( ((n-1)*3 + 1):((n-1)*3 + 3) ); end 有一个更好的方法吗?


回答:
reshape当然是正确的解决方案,如[URL="https://stackoverflow.com/a/793581/5211833"]@gnovice所述[/URL] 。

reshape一个不错的功能是它允许:

A = 1:12; B = reshape(A,4,[]); B = 1 5 9 2 6 10 3 7 11 4 8 12 因此,如果您不知道会有多少列,那么reshape会为您计算出来。同样,如果您不进行修改,则reshape将填充行数。

C = reshape(A,[],4) C = 1 4 7 10 2 5 8 11 3 6 9 12

[url=https://stackoverflow.com/questions/793574]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:32

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.