如何通过Matlab程序将1D转换为2D [英] How to convert 1D to 2D by Matlab program
问题描述
我想问一个关于Matlab程序的问题.
I would like to ask a question about Matlab program.
我有向量a
a = [1 2 3 4 5 6 7 8 9 10 11 12];
我想将向量a
转换为2D数组.通常,我使用此代码对其进行转换.
I would like to convert vector a
to 2D array. Normally, I use this code to convert it.
m =1;
for i=1:4
for j=1:3
b(i,j) = a(m);
m=m+1;
end
end
然后b
是2D矩阵.
b =
1 2 3
4 5 6
7 8 9
10 11 12
任何人,都可以在不使用循环的情况下将1D转换为2D.
Anybody, have an idea to convert 1D to 2D without using loop.
谢谢
推荐答案
Check out the reshape
function and help page.
尤其是
B = reshape(A,m,n)
返回m
-by-c5>矩阵B
,其元素是从A
逐列获取的.如果A
没有m*n
元素,则会导致错误.
returns the m
-by-n
matrix B
whose elements are taken column-wise from A
. An error results if A
does not have m*n
elements.
请注意,它是按列排列的,因此建议您制作一个具有3行4列的矩阵,然后将其倾斜到侧面(A.'
将对矩阵进行转置).
Note that it is column-wise, so I suggest you make a matrix with 3 rows and 4 columns and then tip it on its side (A.'
will take the transpose of a matrix).
这篇关于如何通过Matlab程序将1D转换为2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!