Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a 5 x 5 matrix:

cdsSpread5yrs =

enter image description here

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

rowSpread2 = cdsSpread5yrs[[2]];  
rowSpread2 // MatrixForm

enter image description here

share|improve this question
3  
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

3 Answers 3

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).

share|improve this answer
    
thanks @jVincent, I think your reply is the best so far. Nice and simple. –  sebastian c. Dec 19 '12 at 17:42
1  
@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

 $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

enter image description here

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.

share|improve this answer
    
ok but now extracting all the values in column 1 displays it as a row when I do: cdsSpread5yrs[[All,1]], I am switching over to Matlab soon... –  sebastian c. Dec 19 '12 at 17:07
    
Related post on preprinting with MatrixForm: mathematica.stackexchange.com/a/3099/5 –  rm -rf Dec 19 '12 at 17:08
    
Hi @Nasser, thanks for your reply but in Matlab as you pointed out is rather simple, and it is a valid question so also can't understand why people down voted this question. Also, how to remove this $PrePrint? I tried Remove[$PrePrint] but doesnt seem to work. –  sebastian c. Dec 19 '12 at 17:20
    
@Nasser Thanks Nasser. I hope to learn more about Mathematica but not sure whether to stick with it or use Matlab where things seem more straight forward as I need to hand it project deadline... –  sebastian c. Dec 19 '12 at 17:35
    
@NasserM.Abbasi Very nice edit. –  jVincent Dec 19 '12 at 19:05

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 matrix is a list of lists

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 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 *)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.