Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert nxn matrix to a column vector with (n^2) elements

1,519 views
Skip to first unread message

Tara.An...@gmail.com

unread,
Nov 28, 2007, 6:07:10 AM11/28/07
to
Any ideas how to convert an nxn matrix to a column vector with n^2
elements?

Vassilis Dimitrakas

unread,
Nov 29, 2007, 6:17:20 AM11/29/07
to
Let M be your square matrix. Just type Vector =Flatten[Transpose[M]];

Sseziwa Mukasa

unread,
Nov 29, 2007, 6:22:54 AM11/29/07
to

On Nov 28, 2007, at 5:46 AM, Tara.An...@gmail.com wrote:

> Any ideas how to convert an nxn matrix to a column vector with n^2
> elements?

toColumnVector[x_]:=Transpose[{Flatten[x]}]

Regards,

Ssezi

Jean-Marc Gulliet

unread,
Nov 29, 2007, 6:32:50 AM11/29/07
to
Tara.An...@gmail.com wrote:

> Any ideas how to convert an nxn matrix to a column vector with n^2
> elements?

Beware that Mathematica does not have any special construct to
distinguish row vectors from column vectors. A vector can be represented
by a one dimensional list made of numeric or/and symbolic elements.
Depending on the context and the operation in use, Mathematica
interprets the vector as a column or a row vector. (More generally,
vectors are tensors of rank 1.)

Having said that, I believe that what you are looking for is a command
of the form Flatten@Transpose@m (m being your square matrix) as
illustrated in the following example:

Mathematica 6.0 for Microsoft Windows (32-bit)
Copyright 1988-2007 Wolfram Research, Inc.

In[1]:= m = Array[a, {2, 2}]

Out[1]= {{a[1, 1], a[1, 2]}, {a[2, 1], a[2, 2]}}

In[2]:= %//MatrixForm

Out[2]//MatrixForm= a[1, 1] a[1, 2]

a[2, 1] a[2, 2]

In[3]:= Flatten@Transpose@m

Out[3]= {a[1, 1], a[2, 1], a[1, 2], a[2, 2]}

In[4]:= %//MatrixForm

Out[4]//MatrixForm= a[1, 1]

a[2, 1]

a[1, 2]

a[2, 2]

In[5]:= Flatten@m

Out[5]= {a[1, 1], a[1, 2], a[2, 1], a[2, 2]}

In[6]:= %//MatrixForm

Out[6]//MatrixForm= a[1, 1]

a[1, 2]

a[2, 1]

a[2, 2]

Regards,
--
Jean-Marc

Michael Weyrauch

unread,
Nov 29, 2007, 6:40:00 AM11/29/07
to
Flatten[]

<Tara.An...@gmail.com> schrieb im Newsbeitrag news:fiji4u$ijb$1...@smc.vnet.net...

Bill Rowe

unread,
Nov 29, 2007, 6:47:08 AM11/29/07
to
On 11/28/07 at 5:46 AM, Tara.An...@gmail.com wrote:

>Any ideas how to convert an nxn matrix to a column vector with n^2
>elements?

Transpose@{Flatten@matrix}

Thread[{Flatten@matrix}]

List/@Flatten[matrix]

are just a few possible ways to do it.
--
To reply via email subtract one hundred and four

Szabolcs Horvát

unread,
Nov 29, 2007, 6:57:18 AM11/29/07
to
Tara.An...@gmail.com wrote:
> Any ideas how to convert an nxn matrix to a column vector with n^2
> elements?
>

You should read some of the tutorials on the guide/ListManipulation doc
page. If by a *column vector* you mean a matrix of dimension {n^2, 1},
then both List /@ Flatten[mat] and List /@ Join @@ mat will do it, where
mat is the {n, n} matrix. Omit "List /@" to get a simple vector.

See
<http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/24fa2f19317734fa/69770c22c257ca29#69770c22c257ca29>
on matrices vs vectors in Mathematica.

--
Szabolcs

dh

unread,
Nov 29, 2007, 6:58:19 AM11/29/07
to

Hi,

in mathematica there is usually no need to distinguish between raw and

column vectors. You would simply say: Flatten[matrix] what gives a vector.

Nevertheless, if you want to create a vector of "lines" with one element

you can do this by mapping List to the vector elements:

List /@ Flatten[matrix]

hope this helps, Daniel

0 new messages