如何将矩阵的行连接到向量中?

jim*_*mbo 34 matlab concatenation matrix

对于m-by-m(square)数组,如何将所有行连接成大小为m ^ 2的列向量?

gno*_*ice 64

您可以通过几种不同的方法将矩阵折叠为矢量,具体取决于您希望矩阵的内容如何填充该矢量.这里有两个例子,一个使用函数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
Run Code Online (Sandbox Code Playgroud)

  • `[]`的+1作为`reshape`的参数,不知道. (6认同)