Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 650
jimbo Avatar asked Apr 27 '10 18:04

jimbo


People also ask

How do you make a row vector in a matrix?

Conversion of a Matrix into a Row Vector. This conversion can be done using reshape() function along with the Transpose operation. This reshape() function is used to reshape the specified matrix using the given size vector.

How do I concatenate a row in a matrix MATLAB?

You can also use square brackets to join existing matrices together. This way of creating a matrix is called concatenation. For example, concatenate two row vectors to make an even longer row vector. To arrange A and B as two rows of a matrix, use the semicolon.

What is matrix concatenation?

Matrix concatenation is the process of joining one or more matrices to make a new matrix. The brackets [] operator discussed earlier in this section serves not only as a matrix constructor, but also as the MATLAB concatenation operator. The expression C = [A B] horizontally concatenates matrices A and B .

What does it mean to concatenate vectors?

Description. The Vector Concatenate and Matrix Concatenate blocks concatenate the input signals to create a nonvirtual output signal whose elements reside in contiguous locations in memory. In the Simulink® library, these blocks are different configurations of the same block.


2 Answers

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 
like image 68
gnovice Avatar answered Sep 28 '22 13:09

gnovice


A very important note in changing a matrix to a vector is that , MATLAB produce the output vector form the columns of the matrix, if you use A(:)

for example :

A = [1 2 3 ; 4 5 6]  B = A (:)  B = [1 4 2 5 3 6] 

You can see the direction of changing in the following image. Direction of changing

like image 31
PyMatFlow Avatar answered Sep 28 '22 13:09

PyMatFlow