# Build sparse matrix in mathematica knowing position i,j and value to store, from file

I have a file formatted in this way:

Row Column Value
i    j     Vij


i= INT Number row where in sparse matrix I want the double number Vij

j= INT Number column where in sparse matrix I want the double number Vij

In summary element of sparse matrix {i,j}->Vij

In this file is stored the bottom triangular of large " Lower Triangular SPARSE matrix".

I wish build a sparse Matrix from this file. Moreover, my final SPARSE matrix must be symmetric such that Vij=Vji. Once I imported the file, how di I make my sparse matrix? The command in mathematica is "SparseArray[]", if yes how can I use it ?

I need to make a sparse matrix in mathematica because if I build a classic matrix, the matrix have need a lot of RAM memory and I have a problem of memory overflow.

I try to achieve my goal in this way, but not working:

file=Import["matrix.dat"]
(* Dimension of square matrix *)
dimKall=6000;
s = SparseArray[{file[[i_, 1]],file[[i_, 2]]} -> file[[i_, 3]], {dimKall,dimKall}]


This is a small example of what I want to achieve: file = {{1, 1, 2}, {2, 2, 4}, {3, 3, 7}, {4, 4, 8}, {4, 1, 2}, {3, 2,
10}};
DimKall = 4
s = {{2, 0, 0, 2}, {0, 4, 10, 0}, {0, 10, 7, 0}, {2, 0, 0, 8}};


thx

• @Kuba thank you for your gentle attention ;) Mar 17 '17 at 12:29

m = SparseArray[{#, #2} -> #3 & @@@ file, {DimKall, DimKall}];

M = m + m\[Transpose] - DiagonalMatrix @ Diagonal @ m;

MatrixForm @ M • I guess that SymmetrizedArray is also relevant. Mar 17 '17 at 19:56
• @LLlAMnYP I suppose it is even better I just wasn't aware it is there ;)
– Kuba
Mar 17 '17 at 19:59
• Instead of subtracting: m + Transpose[LowerTriangularize[m, -1]]. Mar 21 '17 at 14:30