10

It shows me like {{0}, {1}, {0}, {-1}} . Is it possible to make it look like a vector, always?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Gappy Hilmore
  • 1,253
  • 1
  • 9
  • 17

2 Answers2

22

Add this to your notebook or init file

    $PrePrint = If[MatrixQ[#], MatrixForm[#], #] &;

Then all matrices will automatically display as MatrixForm

Mathematica graphics

and

Mathematica graphics

If you want to format lists as column vectors also, try

$PrePrint = 
  Which[MatrixQ[#], MatrixForm[#], VectorQ[#], ColumnForm[#], 
    True, #] &;

Now also

Mathematica graphics

Nasser
  • 143,286
  • 11
  • 154
  • 359
4

As @kattern pointed out, MatrixForm will pretty print your lists to look like matrices.

{{0}, {1}, {0}, {-1}} // MatrixForm

Mathematica graphics

A word of caution, however: MatrixForm can get in the way of your calculations if you are not careful. See this question and the related answers: Why does MatrixForm affect calculations?.

For instance, you could get bitten by something like this:

matWRONG = {{0}, {1}, {0}, {-1}} // MatrixForm
(matCORRECT = {{0}, {1}, {0}, {-1}}) // MatrixForm

Mathematica graphics

Both expressions print out a nice formatted version of your vector, but there is a subtle difference.

  • In the first case, you assigned the graphical output of MatrixForm, i.e. the pretty-printed form, to the variable matWRONG. That's probably not what you meant to do.
  • The second version assigns the vector to the matCORRECT variable, then pretty-prints the value of that variable. The variable itself contains the vector, and can be further operated upon.

For instance, you can transpose the matCORRECT vector, but if you try to do the same on the matWRONG one, Transpose will not recognize its input as a vector and return unevaluated, and you will probably be confused.

Transpose[matCORRECT]
Transpose[matWRONG]

Mathematica graphics

MarcoB
  • 67,153
  • 18
  • 91
  • 189
  • would the macro he wrote cause any problems like this? – Gappy Hilmore May 18 '15 at 03:10
  • @grdgfgr As far as I know, it won't. In my understanding, in that case the pretty-printing will happen "last", i.e. after the assignments etc have already been taken care of. – MarcoB May 18 '15 at 03:28