对于一个m×m(方形)数组,如何将所有行连接成大小为m^2的列矢量?
有几种不同的方法可以将矩阵折叠成向量,具体取决于您希望矩阵的内容如何填充该向量。以下是两个示例,一个使用函数reshape
(在首先转置矩阵之后),另一个使用冒号语法(:)
:
>> M = [1 2 3; 4 5 6; 7 8 9]; % Sample matrix
>> vector = reshape(M.', [], 1) % Collect the row contents into a column vector
vector =
1
2
3
4
5
6
7
8
9
>> vector = M(:) % Collect the column contents into a column vector
vector =
1
4
7
2
5
8
3
6
9