# MatrixForm explanation as why row extract is displayed as a column?

I have a 5 x 5 matrix:

But after doing a row extract, why is it displaying as a column?

rowSpread2 = cdsSpread5yrs[[2]];


-
What you extract has the form {x1,..,x5} and that by default is displayed as a column, you can put braces around it if you wish to display it as a row {cdsSpread5yrs[[2]]}//MatrixForm or cdsSpread5yrs[[{2}]]//MatrixForm – ssch Dec 19 '12 at 15:45
Makes it confusing to know whether I am dealing with a column or row... – sebastian c. Dec 19 '12 at 16:56
You might find this tutorial useful: reference.wolfram.com/mathematica/tutorial/… – chuy Dec 19 '12 at 17:14

Think of it this way, a matrix is a rectangular set of elements: m = {{a, b, c}, {d, e, f}}, and the first row m[[1,All]] has the list of elements {a,b,c}, the first column m[[All,1]] has the list of elements {a,d}

Now if I ask Mathematica to plot on matrix form both {a,b,c} and {a,b}, how on earth should it know whether I got those lists of elements from a row or a column? Or I could have just typed them in. What it needs to do is to interpret them as a column (eg {{a},{b},{c}}) or a row (eg {{a,b,c}}). The default is to interpret it as a column.

What you can do is that when you need to extract stuff write it out

 matrix = {{a, b, c}, {d, e, f}};
matrix [[{2} , All]]   (* => {{b}, {e}}   which is all rows in column 2*)
matrix [[All , {2}]] (* => {{d, e, f}}  which is all columns in row 2*)


This way you retain the information of whether it's a column or a row you are dealing with. (And yes of cause in the first case [[{2},;;]] the last part is redundant, as it's the default).

-
thanks @jVincent, I think your reply is the best so far. Nice and simple. – sebastian c. Dec 19 '12 at 17:42
@NasserM.Abbasi This isn't a trick. – jVincent Dec 19 '12 at 17:55
@jVincent Should m[[All,1]] not give {a,d} instead of {a,b} – Lou Dec 19 '12 at 18:36
@Lau Yes, Thank you. – jVincent Dec 19 '12 at 19:00
@NasserM.Abbasi No offence taken! I just wanted to clarify that this isn't some cleaver trick that makes it come out like one would want it. It's the intended behavior and quite consistent. I sometimes feel like MATLABs defaults is a trick based on the assumption that people mainly deal with 2D matrices, thus they don't have lists of numbers, only rows or columns. – jVincent Dec 19 '12 at 19:03

Well, to answer you comment For that extra work might

I just wanted to say that Mathematica is really a very flexible language (may be too flexible:)

If you do not like something, you could always write little code to customize things.

## Fully updated answer

Seeing the excellent solution by jVincent below, I thought I should re-write eveything again to make this easier and more directed answer.

To obtain the same display as one can with Matlab, follow these 2 simple steps

### A simple list is neither a row nor a column

A simple list is just a collection of elements and cannot be transposed like a row/column. Indeed, you can see for yourself that it does not have a second singleton dimension which is necessary for a row/column vector.

Dimensions[a = Range@5]
(* {5} *)

Transpose@a
(* Transpose::nmtx: The first two levels of the one-dimensional list {1,2,3,4,5}
cannot be transposed. >> *)


(Note: these apply to ragged lists too, but I'll not address that here.)

Contrast this with the behaviour for a row/column vector:

Dimensions[a = {Range@5}]
(* {1, 5} *)

Transpose@a
(* {{1}, {2}, {3}, {4}, {5}} *)

Dimensions@%
(* {5, 1} *)


You can see that these have the second dimension and can be transposed back and forth. However, you can:

### Use Part directly to get the column/row vector

As I mentioned above, when you do a[[All, 1]] what you're really asking for are the elements in the first position in all the sublists. However, if you instead wrap {} around your index 1, then as the documentation says, you get back a list of the parts. This list of parts introduces the second singleton dimension that then transforms the simple list into a corresponding row/column vector as the case may be. For example:

a = Range@9 ~Partition~ 3;
a[[All, {1}]] // MatrixForm (* This is a column vector *)


a[[{1}, All]] // MatrixForm (* This is a row vector *)


-