From: Priom Rahman on
Hi guys,

I was wondering if someone could suggest a way to turn a matrix to a single vector in matlab (ie is there a function?) efficiently.

I was using “for” statements (& and looping for the dimensions of the matrix) but with a lot of data, the runtime is excessive.

Basically what I want to do is turn:

M = [ 1 2 3; 4 5 6; 7 8 9; 10 11 12]
to
m = [ 1 2 3 4 5 6 7 8 9 10 11 12]'

is that a quick way to do this? (my matrix is 18000 x 244) and I want it to be a single stream of data (4392000) long.

Thanks for your help.
From: Matt Fig on
m = reshape(M.',1,[])
From: dpb on
Priom Rahman wrote:
....

> Basically what I want to do is turn:
>
> M = [ 1 2 3; 4 5 6; 7 8 9; 10 11 12] to m = [ 1 2 3 4 5 6 7 8 9 10 11 12]'
>
> is that a quick way to do this? ...

If you do want it in row-major order alternatively,

m=M';m=m(:)';

Unfortunately, another place where sequential operation notation would
be useful because

m=M(:);

results in [1 4 7 ...]' instead (natural storage order is column-wise) and

m=M'(:); % is illegal syntax.

--
From: James Tursa on
dpb <none(a)non.net> wrote in message <i41rmf$tjn$1(a)news.eternal-september.org>...
>
> If you do want it in row-major order alternatively,
>
> m=M';m=m(:)';
>
> Unfortunately, another place where sequential operation notation would
> be useful because
>
> m=M(:);
>
> results in [1 4 7 ...]' instead (natural storage order is column-wise) and
>
> m=M'(:); % is illegal syntax.

The latter might be useful notationally but even if it were legal syntax it wouldn't save you much since the two-statement solution you list above only has a total of one data copy (for the transpose). The m=m(:)' should be pretty quick since there would be no data copy involved.

James Tursa
From: Priom Rahman on
Thanks guys for your promt replies !

both of those worked !!

m=M';m=m(:)';
&
m =reshape(M.',1,[])

=)