如何将矩阵的行连接成向量? [英] How do you concatenate the rows of a matrix into a vector?

查看:72
本文介绍了如何将矩阵的行连接成向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于m×m(正方形)数组,如何将所有行连接到大小为m ^ 2的列向量中?

For an m-by-m (square) array, how do you concatenate all the rows into a column vector with size m^2 ?

推荐答案

有几种不同的方法可以将矩阵折叠为向量,具体取决于您希望矩阵的内容如何填充该向量.这是两个示例,一个示例使用功能 reshape (先转置矩阵之后),然后使用冒号语法 (:):

There are a couple of different ways you can collapse your matrix into a vector, depending upon how you want the contents of your matrix to fill that vector. Here are two examples, one using the function reshape (after first transposing the matrix) and one using the colon syntax (:):

>> 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

这篇关于如何将矩阵的行连接成向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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