# Self-defined function to transform row vector into column vector, where am I wrong? [closed]

I defined a function row2col[] to transform row into column, Where am I wrong?

 row2col[args__] :=
Module[
{argList = {args}, n = Length[{args}], i},
col = {};
For[i = 1, i <= n, i++,
midd = Map[List, argList[[i]]];
col = Join[col, midd];
];
col
];
col = row2col[{1, 2, 3}, {4, 5, 6}, {7, 8, 9}];
MatrixForm[col]
Quit[];


The expexted result is

(*{{{1},{2},{3}},{{4},{5},{6}},{{7},{8},{9}}}*)


I know other simple command can return the result i need, I want to know what's wrong with my definition.

-

## closed as too localized by Yves Klett, m_goldberg, whuber, Sjoerd C. de Vries, ArtesMar 15 '13 at 20:55

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

What result are you expecting from this? –  RunnyKine Mar 15 '13 at 5:07
You have not included the semi-colon after the For loop, though it's not entirely clear what you are trying to do. –  Jonathan Shock Mar 15 '13 at 5:08
In general if you wish to change rows to columns you can use Tranpose[] –  Jonathan Shock Mar 15 '13 at 5:09
@JonathanShock What trips up beginners is that you cannot transpose a simple list. It has to be a transposable list of depth 3... such as {{1,2,3}} –  rm -rf Mar 15 '13 at 5:29
Please see mathematica.stackexchange.com/q/18393/193 for more avoidable pitfalls –  belisarius Mar 15 '13 at 7:52

Using your function definition, since you insist on it, adding Partition will give you what you want.

row2col[args__] :=
Module[{argList = {args}, n = Length[{args}], i}, col = {};
For[i = 1, i <= n, i++, midd = Map[List, argList[[i]]];
col = Join[col, midd];]; Partition[col, 3]];


Then

row2col[{1, 2, 3}, {4, 5, 6}, {7, 8, 9}]


returns

(* {{{1}, {2}, {3}}, {{4}, {5}, {6}}, {{7}, {8}, {9}}} *)


But if you care to learn Mathematica then think about using Transpose to write a one-liner for this that'll also be faster.

-
RunnyKine, You are right, but Transpose doesn't support single row vector, is it? e.g. Transpose[{1,2,3}] error occurs. –  novice Mar 15 '13 at 5:34
@user5463, you need an additional bracket when using Transpose. So it's Transpose[{{1,2,3}}]. –  RunnyKine Mar 15 '13 at 5:51
RunnyKine,what if I don't use Partition[]. why using Join[col, midd, 2] is wrong. –  novice Mar 15 '13 at 6:33
@user5463 It's not entirely clear what you want but please read these threads: (189), (SO 7537401), (14494) –  Mr.Wizard Mar 15 '13 at 6:44

For loops are not usually the way to use Mathematica. See:
Alternatives to procedural loops and iterating over lists in Mathematica

Based on your given input and output you might use:

row2col[a__List] := Map[List, {a}, {2}]

row2col[{1, 2, 3}, {4, 5, 6}, {7, 8, 9}]

{{{1}, {2}, {3}}, {{4}, {5}, {6}}, {{7}, {8}, {9}}}


This also handles variable length inputs, e.g.:

row2col[{1, 2}, {4, 5, 6}, {7}]

{{{1}, {2}}, {{4}, {5}, {6}}, {{7}}}


If your input is rectangular you can use Transpose for greater efficiency.
Here it is better to give input as an array rather that a series of arguments:

Clear[row2col]

row2col[a_?MatrixQ] := Transpose[{a}, {3, 1, 2}]

row2col[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}]

{{{1}, {2}, {3}}, {{4}, {5}, {6}}, {{7}, {8}, {9}}}

-
Mr Wizard, Wonderful, I didn't notice Map[] has an option to select the layer –  novice Mar 15 '13 at 7:34