Is there any function opposite to Normal? For example, let as say that I have an SparseMatrix A, then, let as say that for convenience for a certain operation like Packing, I convert it into an Array by doing


and afterwards, I want A to became an SparseArray again.

Is there anyway to do this without the need to define the matrix A again?


  • $\begingroup$ In case there is no such function, a way to solve this problem is to define A_backup =A, then, A=Normal[A] and afterwards A=A_backup. $\endgroup$
    – Mencia
    Dec 15, 2013 at 17:41
  • 4
    $\begingroup$ SparseArray[A] $\endgroup$
    – ssch
    Dec 15, 2013 at 17:42
  • $\begingroup$ @ssch thankyou! $\endgroup$
    – Mencia
    Dec 15, 2013 at 17:44

1 Answer 1


If the background value is 0 just run SparseArray on it:

sp = SparseArray[Band[{1, 1}] -> 1, {100, 100}];
(* 2152 *)

nrm = Normal[sp];
(* 40168 *)

sp2 = SparseArray[nrm];
(* 2152 *)

If it has another background value do SparseArray[nrm, Automatic, background], if you don't know the background value use the commonest element, which is First@Commonest[Flatten@nrm]

  • $\begingroup$ what is the background value? $\endgroup$
    – Mencia
    Dec 15, 2013 at 17:49
  • $\begingroup$ @Mencia the default value that is used where nothing else is specified in the SparseArray e.g. SparseArray[Band[{1,1}]->9, {5,5}, Infinity]//Normal//MatrixForm $\endgroup$
    – ssch
    Dec 15, 2013 at 17:54
  • $\begingroup$ but the best is to specify them as zero right? since zero terms will not take up memory in the SparseArray $\endgroup$
    – Mencia
    Dec 15, 2013 at 17:59
  • 3
    $\begingroup$ @Mencia Doesn't matter what it is, SparseArray only stores information representing position and value for places where the value is not equal to the background value, so for memory use it doesn't matter what the background happens to be. However 0 is very common and is usually what you want (this is why Mathematica assumes you want 0 unless you specify otherwise) $\endgroup$
    – ssch
    Dec 15, 2013 at 18:04
  • $\begingroup$ @Mencia Here's link to some documentation: tutorial/SparseArrays-ManipulatingLists. It implies what ssch says here, but it's not as clear as this answer. :) $\endgroup$
    – Michael E2
    Dec 15, 2013 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.