How to turn a matrix into a vector?

12 views (last 30 days)
Amanda Queiroz
Amanda Queiroz on 20 Dec 2021
Answered: Voss on 20 Dec 2021
I have a matrix with 1000 rows and 1000 columns. And I want to turn it into a vector, that is, putting all the values ​​in a single column and assigning a value (index) to each one. How would I do this? Thank you.

Accepted Answer

Voss
Voss on 20 Dec 2021
I'm not sure what you mean by "assigning a value (index) to each one", but you can use the colon operator to put all the values into a single column:
A = magic(3);
B = A(:);
display(A);
A = 3×3
8 1 6 3 5 7 4 9 2
display(B);
B = 9×1
8 3 4 1 5 9 6 7 2
As you can see from the output, B is a column vector containing the elements of A in order of: 1st column of A, 2nd column of A, and so on.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!