2
$\begingroup$

If I have a matrix:

row 1)1 0 0 4
row 2)0 0 0 0
row 3)5 1 0 0
row 4)0 5 0 0
row 5)0 0 0 9

How would I put it in a sparse matrix format:

a(0)(0) = ? a(0)(1) = ? a(0)(2) = ?
a(1)(0) = ? a(1)(1) = ? a(1)(2) = ?
a(2)(0) = ? a(2)(1) = ? a(2)(2) = ?
a(3)(0) = ? a(3)(1) = ? a(3)(2) = ?
a(4)(0) = ? a(4)(1) = ? a(4)(2) = ?
a(5)(0) = ? a(5)(1) = ? a(5)(2) = ?

I've tried looking on the internet but I couldn't find any help telling me how to do it.

$\endgroup$
2
  • $\begingroup$ (I believe this is a dupe, but I can't find the other thread.) Apply SparseArray[] to your original matrix to get the sparse form, e.g. SparseArray[IdentityMatrix[4]]. $\endgroup$ Mar 5, 2016 at 1:18
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Mar 5, 2016 at 2:15

3 Answers 3

3
$\begingroup$

With

a = {{1, 0, 0, 4},
  {0, 0, 0, 0},
  {5, 1, 0, 0},
  {0, 5, 0, 0},
  {0, 0, 0, 9}};

Then

sa = SparseArray[a]

Since SparseArray takes the default to be 0.

Hope this helps.

$\endgroup$
2
$\begingroup$

According to the link (How to | Work with Sparse Matrices) : "https://reference.wolfram.com/language/howto/WorkWithSparseMatrices.html". Hope this help.

mat = SparseArray[{{1, 0, 0, 4}, {0, 0, 0, 0}, {5, 1, 0, 0}, {0, 5, 0,0}, {0, 0, 0, 9}}]

mat // MatrixForm
$\endgroup$
1
$\begingroup$
a = SparseArray[{{1,1} -> 1, {3,1} -> 5, ....}]
$\endgroup$

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