3
$\begingroup$

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:

small example

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

$\endgroup$
1
  • 1
    $\begingroup$ @Kuba thank you for your gentle attention ;) $\endgroup$
    – plus91
    Mar 17, 2017 at 12:29

1 Answer 1

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

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

MatrixForm @ M

enter image description here

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

Your Answer

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

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