How to turn a matrix into a vector?

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

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

Asked:

on 20 Dec 2021

Answered:

on 20 Dec 2021

Community Treasure Hunt

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

Start Hunting!