如何在 MATLAB 中调整矩阵的大小? [英] How do I resize a matrix in MATLAB?

查看:390
本文介绍了如何在 MATLAB 中调整矩阵的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 1×12 矩阵,我想将其调整为 4×3 矩阵.我怎么能这样做?

Suppose I had a 1-by-12 matrix and I wanted to resize it to a 4-by-3 matrix. How could I do this?

我目前的解决方案有点丑:

My current solution is kind of ugly:

for n = 1:(length(mat)/3)
    out(n,1:3) = mat( ((n-1)*3 + 1):((n-1)*3 + 3) );
end

有没有更好的方法来做到这一点?

Is there a better way to do this?

推荐答案

reshape 当然是正确的解决方案,正如 @gnovice.

reshape is of course the proper solution, as stated by @gnovice.

reshape 的一个很好的特性是它允许:

A nice feature of reshape is that it allows this:

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

因此,如果您不知道将有多少列,reshape 会为您计算.同样,reshape 将填充行数,如果您省略它.

So if you don't know how many columns there will be, reshape will compute it for you. Likewise, reshape will fill in the number of rows, if you leave that out.

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

这篇关于如何在 MATLAB 中调整矩阵的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆