Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

matlab - How do you concatenate the rows of a matrix into a vector?

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...