matlab中怎样将一维数组转化为二维矩阵?

如题所述

    可以用reshap(),也可以直接“捋直”了。

    为了清晰点,给你举个例子吧:

    a=[1,2;3,4;];

    b=a(:);

    c=reshape(a,[],1);

    得到的b,c都是一样的一维列向量。

reshape介绍:

reshape函数重新调整矩阵的行数、列数、维数。在matlab命令窗口中键入docreshape或helpreshape即可获得该函数的帮助信息。

用法:

B = reshape(A,m,n)

B = reshape(A,m,n,p,...)

B = reshape(A,[m n p ...])

B = reshape(A,...,[ ],...)

B = reshape(A,siz)

程序示例:

close all; clear; clc;

A = [1 2 3; 4 5 6; 7 8 9; 10 11 12] % 4 by 3

B = reshape(A, 2, 6) % 2 by 6

% C = reshape(A, 2, 4) % error

% D = reshape(A, 2, 10) % error

E = reshape(A, 2, 3, 2) % 2 by 3 by 2

注意:reshape函数对原数组的抽取是按照列抽取的(对原数组按列抽取,抽取的元素填充为新数组的列)

温馨提示:答案为网友推荐,仅供参考