# About how to speed up in assignment

I would like to know if there is a way for increasing the speed of the following computation: I have a list, let's call it list, and another list of values, val. I wish to create a sparse matrix in the following way: The only nonzeros elements of the matrix are the positions of list (2 components) where the value is val[[i]]. I have generate this code: Mathematica

list = {{1, 3}, {1, 4}, {5, 5}, {5, 6}, {6, 2}, {6, 3}, {6, 4}, {6, 5}, {6, 6}, {7, 3}, {7, 4}, {7, 5}, {7, 6}, {8, 3}, {8, 5}, {8, 6}};

val = Table[i^2, {i, Length[list]}];

matrix = Table[0., {i, 13}, {j, 13}];(* 13 is just an example *)

For[i=1,i<= Length[list], i++, matrix[[list[[i]][[1]], list[[i]][[2]] ]] = val[[i]]];

Do you know a way for improving the speed? I ask this because if the length of list is long the loop requires much time to do.

sa = SparseArray[list -> val]


TeXForm @ MatrixForm @ sa


$$\left( \begin{array}{cccccc} 0 & 0 & 1 & 4 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 9 & 16 \\ 0 & 25 & 36 & 49 & 64 & 81 \\ 0 & 0 & 100 & 121 & 144 & 169 \\ 0 & 0 & 196 & 0 & 225 & 256 \\ \end{array} \right)$$

Use the second argument to specify the desired dimensions: For example, to get a sparse matrix with 10 rows and 9 columns, use

sa2 = SparseArray[list -> val, {10, 9}];

TeXForm @ MatrixForm @ sa2


$$\left( \begin{array}{ccccccccc} 0 & 0 & 1 & 4 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 9 & 16 & 0 & 0 & 0 \\ 0 & 25 & 36 & 49 & 64 & 81 & 0 & 0 & 0 \\ 0 & 0 & 100 & 121 & 144 & 169 & 0 & 0 & 0 \\ 0 & 0 & 196 & 0 & 225 & 256 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$$

• Wow! Many thanks, kglr. One more question: What about if val is a matrix instead a list, call it M, and I want to assign matrix[[list[[i]][[1]], list[[i]][[2]] ]] = M[[list[[i]][[1]], list[[i]][[2]] ]] ? Commented Apr 24, 2023 at 14:59
• @Benigno, use SparseArray[list -> Extract[M, list]]?
– kglr
Commented Apr 24, 2023 at 15:06
• I think we can close this question with the two great answers of kglr. Commented Apr 24, 2023 at 15:16
m = SparseArray[MapIndexed[# -> #2[[1]]^2 &, list], {13, 13}];

m // MatrixForm


$$\left( \begin{array}{ccccccccccccc} 0 & 0 & 1 & 4 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 9 & 16 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 25 & 36 & 49 & 64 & 81 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 100 & 121 & 144 & 169 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 196 & 0 & 225 & 256 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$$