I have a 5 x 5 matrix:
cdsSpread5yrs =
But after doing a row extract, why is it displaying as a column?
rowSpread2 = cdsSpread5yrs[[2]];
rowSpread2 // MatrixForm
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).
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.
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
$PrePrint = If[MatrixQ[#], TraditionalForm[#], #] &;
and
Each time you want to disply a single row
or a single column
just remember to add {}
around the index value. Otherwise, follow normal coding as before.
With the above simple rule, now Mathematica will print the same thing as Matlab (and it looks even better since it uses TraditionalForm
Here are some examples side-by-side with Matlab
ps. I do not consider this as answer, since I using jVincent solution to show how to use it only. jVincent deserves the credit for this.
MatrixForm
: mathematica.stackexchange.com/a/3099/5
$\endgroup$
The reason why you get that output can be understood by looking at how Mathematica deals with matrices and what the Part
operation really does.
A List
is a generic container in Mathematica that holds elements (which can be anything).
When dealing with matrices in Mathematica, there are no "rows" and "columns" — it's just a list of lists. We assign meanings to elements in this structured 2D array and call the $n^{th}$ element in each sublist to be in the $n^{th}$ row and all the elements in the $m^{th}$ list to be in the $m^{th}$ column.
Using Part
to access the $n^{th}$ row/column as you have done in the question, will extract the elements of that row/column and Mathematica puts it in a List
. However, this is a simple list, which behaves differently from a row or column vector. Specifically:
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:
Part
directly to get the column/row vectorAs 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 *)
{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
orcdsSpread5yrs[[{2}]]//MatrixForm
$\endgroup$