Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make MatrixForm display row horizontally as a real row vector and not vertically as a column vector?

Is there a way to make MatrixForm display a row vector horizontally on the line and not vertically as it does for column vectors? As this confuses me sometimes. Do you think it will be hard to write wrapper around matrix form to adjust this behavior?

For example, here is a 2 by 2 matrix. The rows display the same as the columns. Both are shown vertical.

enter image description here

Question: Is it possible to make MatrixForm display row vectors laid out horizontally and not vertically?

Sorry if this was asked before, a quick search shows nothing.

thanks

update (1)

fyi, this is in Matlab, it prints rows horizontally and column vertically automatically, I was hoping for something like this. But I'll use suggestion by Heike below for now as it solves this at the cost of little extra typing.

enter image description here

update (2) Using Hilderic solution is nice also, I always had hard time printing 3D matrix in a way I can read it. Here it is now using the {} trick

enter image description here

like image 424
Nasser Avatar asked Jan 02 '12 21:01

Nasser


2 Answers

For both arrayname[[All,1]] and arrayname[[1,All]], Part delivers a vector, and MatrixForm has no way of determining which "orientation" it has. Accordingly, it always prints vectors as columns.

About the only thing you can do is provide your own output routine for row vectors, e.g., by wrapping it in an enclosing list, converting it back to a (single-row) matrix:

rowVector[a_List] := MatrixForm[{a}]
columnVector = MatrixForm   (*for symmetry*)

It's still up to you to remember whether a vector came from a row or a column, though.

like image 97
Hilderic Browne Avatar answered Dec 31 '22 20:12

Hilderic Browne


Or you could just cook up your own RowForm function, e.g.:

RowForm[(m_)?VectorQ] := Row[{"(",Row[m,"  "], 
     ")"}, "\[MediumSpace]"]; 

Then

RowForm[twoRowsMatrix[[All,1]]]

looks kind of o.k.

Alternatively, if you really just care about displaying vectors, you could do:

twoRowsMatrix = {{a11, a12}, {a21, a22}};

TakeColumn[m_?MatrixQ, i_] := (Print[MatrixForm[#]]; #) &@m[[All, i]];
TakeRow[m_?MatrixQ, i_] := (Print[MatrixForm[{#}]]; #) &@m[[i]];
TakeColumn[twoRowsMatrix, 1]
TakeRow[twoRowsMatrix, 1]
like image 44
Rolf Mertig Avatar answered Dec 31 '22 18:12

Rolf Mertig