How to Convert Matrix to Vector in MATLAB

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.

Students and professionals alike depend on the MATLAB computer software program from MathWorks to input, analyze, plot and share numerical data. The program is especially useful in the field of Linear Algebra, which involves vectors and matrices. A vector is simply a list of numbers. A matrix is similar but contains multiple rows and columns of numbers. MATLAB contains a built-in function to reshape matrices that you can use to turn any matrix into a single row -- a vector.

Advertisement

Step 1

Define a matrix in a standard way, if you haven't already done so, by typing for example the following: A = [1 2 3; 4 5 6; 7 8 9; 5 5 5];

Video of the Day

This code creates a matrix 'A' that is four rows by three columns.

Advertisement

Step 2

Count the number of elements (numbers) in the matrix automatically and store it in a variable 'S' with the following code: s = size(A); S = s(1)*s(2);

Advertisement

Step 3

Reshape the matrix 'A' into a vector 'V' by typing the following code: V = reshape(A,1,S)

The 'reshape' function reshapes the matrix 'A' into a new matrix with 1 row and 'S' columns - a vector.

Video of the Day

Advertisement

Advertisement

references