> Any ideas how to convert an nxn matrix to a column vector with n^2
> elements?
toColumnVector[x_]:=Transpose[{Flatten[x]}]
Regards,
Ssezi
> 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
<Tara.An...@gmail.com> schrieb im Newsbeitrag news:fiji4u$ijb$1...@smc.vnet.net...
>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
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
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