convert into a single row matrix [duplicate]
I have a matrix say a = [1 5 9;7 8 5; 7 1 4];
I want to make a linear matrix of a
i.e., a1 = [1 5 9 7 8 5 7 1 4];
a'(:)' %# Octave
b= a'; b(:)' %# Matlab
For more information on column-major order and on colon.
Added, more verbose variations may be occasionally practical as well:
a'(ind2sub([3 3], 1: 9))
permute(a, [2 1])(ind2sub([3 3], 1: 9))
Here permute(a, [2 1])
is now equivalent to a.'
.
One more variant
a = reshape( a.', 1, numel(a) )
Note use of .'
to get the non-conjugated TRANSPOSE - '
corresponds to CTRANSPOSE
This is how you do it in Matlab
a1 = a(:);
Or if you need to go by rows, transpose it before and after:
b = a';
b1 = b(:);
a1 = b1';
精彩评论