7
$\begingroup$

It is very simple in Mathematica to convert a list of lists into a matrix: all you need to do is apply the //MatrixForm function to it and voila.

Is it possible to do the reverse though? I have a matrix that I want to convert back into a list of lists, so as to manipulate its elements more easily. Is this possible? Is there a 'ListForm' function that deletes all MatrixForms from the element in question?

As an example, here is the FullForm for one of my elements: MatrixForm[List[List[63],List[4,62]]]

Is there a function which would take this as input and return simply List[List[63],List[4,62]]?

Thank you

$\endgroup$
4
  • 1
    $\begingroup$ You should go through this section of the documentation. The things covered there are essential for using Mathematica, and will also answer your question. $\endgroup$
    – Szabolcs
    Commented Mar 2, 2014 at 14:53
  • $\begingroup$ possible duplicate of Why does MatrixForm affect calculations? $\endgroup$ Commented Mar 2, 2014 at 14:57
  • $\begingroup$ Link to W Community version $\endgroup$
    – Szabolcs
    Commented Mar 2, 2014 at 15:47
  • 1
    $\begingroup$ another approach to maintaining your underlying data structure but still using MatrixForm for display is to enclose in brackets ()s e.g. (x={{1,2,3},{4,5,6}})//MatrixForm. $\endgroup$
    – PlaysDice
    Commented Mar 2, 2014 at 21:54

1 Answer 1

13
$\begingroup$

I'll start with the standard warning: MatrixForm is just a wrapper that makes your matrices look pretty. Nothing more, nothing less. It does not "convert" a list of lists to a matrix. Your list of lists is already a matrix:

m = Identity@10;
MatrixQ@m
(* True *)

Using MatrixForm wrapped matrices in calculations will only give you an error. Use it only for typesetting/display purposes.


To answer your specific question, if you have a MatrixForm wrapped around it for whatever reason, you can remove the wrapper in one of the following ways:

First@m (* or *)
Identity @@ m (* or *)
m /. MatrixForm[x_] :> x

which will give you back the list of lists.

$\endgroup$
3
  • 3
    $\begingroup$ Why not First? $\endgroup$
    – Szabolcs
    Commented Mar 2, 2014 at 14:48
  • $\begingroup$ @Szabolcs Heh, why not indeed! :) I'll add it in. $\endgroup$
    – rm -rf
    Commented Mar 2, 2014 at 14:51
  • 2
    $\begingroup$ While we´re at it, why not m[[1]]? $\endgroup$
    – Yves Klett
    Commented Mar 2, 2014 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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