How to transform an array from n x m into nm x 1?

Hello,
I am quite new and fresh on matlab, had a couple weeks of trial and error with it... Anyways, I have this little problem thats in the way between struggle and finished coursework.
PROBLEM.
Lets say i have a matrix,
A = [1,2,3; 4,5,6; 7,8,9] % dimensions 3x3
DESIRED SOLUTION. Is there an easy way to turn it into:
B = [1;2;3;4;5;6;7;8;9] % dimensions 9x1
THANK YOU a lot in advance :)

2 Comments

HI Gediminas, Yes there is. If you use B = A(:), then B is a column vector that is read out columnwise from A, i.e. first column first, second column second, etc. . That gives A = [1;4;7;2;5;8;3;6;9]. But you want A read out rowwise, so you transpose A first:
A = A.';
B = A(:)

Sign in to comment.

 Accepted Answer

B = A.';
B = B(:);
or
B = reshape(A.',[],1);
In both cases, the transpose is necessary to get the elements of A to line up in memory in the desired order.

More Answers (0)

Categories

Find more on Programming 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!