# Generating a (sparse?) array from data

I have a list:

{{pos_1,val_1},{pos_2,val_2},...}


and would like to generate an array of dimension d, with values val_i at positionspos_i, the rest being filled with 0. I would assume that I shall use SparseArray but this function has a different form of argument:

{pos_1->val_1,pos_2->val_2,...}


Is there any simple way to transform one type of the argument into the other, or completely different approach shall be undertaken?

Another possibility is

SparseArray[Rule @@ Transpose[posval]]


I think the input syntax that gets processed the quickest among the documented ones would be

SparseArray[pos -> vals , dims]


where pos is a list of positions and vals is a list of values. This way, pos and vals can be PackedArrays which can be processed much faster. A list of the form you described cannot be packed in general. So better not generate it this way. Mapping Rule on the position-value pairs also unpacks arrays. Generate pos and vals independently. In general, this will be much faster.

• Your suggestion works. Thank you! I just have noted another problem. In my array some positions are 0's. For simplicity I would just increment all positions by 1. At which stage it would be most efficient? Is it possible at the stage of generating the SparseArray? – drer Feb 28 '18 at 15:36
• You can use {pos,vals} = Transpose[posval]; SparseArray[pos+1 -> vals , dims]. This adds 1 onto all positions indices. – Henrik Schumacher Feb 28 '18 at 15:54
• Thank you. It works. – drer Feb 28 '18 at 16:45
• You're welcome! – Henrik Schumacher Feb 28 '18 at 17:08
posval = Join[List /@ RandomSample[Tuples[Range[5], {2}], 10],
List/@RandomInteger[9, 10], 2]


{{{2, 4}, 3}, {{2, 1}, 8}, {{3, 5}, 4}, {{2, 3}, 3}, {{5, 5}, 4}, {{3, 2}, 8}, {{4, 5}, 8}, {{3, 1}, 6}, {{2, 5}, 2}, {{5, 1}, 8}}

dims = Max /@ Transpose[posval[[All, 1]]];
sa = SparseArray[Rule @@@ posval, dims];


SparseArray[<10>, {5, 5}]

TeXForm @ MatrixForm @ sa


$$\left( \begin{array}{ccccc} 0 & 0 & 0 & 0 & 0 \\ 8 & 0 & 3 & 3 & 2 \\ 6 & 8 & 0 & 0 & 4 \\ 0 & 0 & 0 & 0 & 8 \\ 8 & 0 & 0 & 0 & 4 \\ \end{array} \right)$$

For a list of the form: list={{pos_1,val_1},{pos_2,val_2}}

How about SparseArray[Map[#[[1]] -> #[[2]] &, list]] ?

• I have tried and got the message List expected at position 1 in SparseArray. Can you give a working example? – drer Feb 28 '18 at 15:22
• sorry made a typo in the equation. I will add a full working example when I get access to MMA again – Dunlop Feb 28 '18 at 15:55