Suppose I have a matrix M
, a list
list= {{1,1}, {1,3}, {2,3}};
and a list of
values = {a,b,c}
How do I set the matrix elements {M[[1,1]], M[[1,3]], M[[2,3]]} = values
?
This will produce the list elements I want:
Map[P[[Apply[Sequence, #]]] &, list] = {M[[1,1]], M[[1,3]], M[[2,3]]}
but the final command is Map
, which is protected, and hence I cannot use it to set the list elements to the elements of values.
Of course one way of doing this is by looping,
For[i=1, i<=Length[list], i++, M[[Sequence @@ list[[i]] = values[[i]]]
However, since I would like to work with very large lists of indices I want to avoid loops as much as possible.
{M[[1, 1]], M[[1, 3]], M[[2, 3]]} = values
? Or do you want it automated? $\endgroup$For[i=1, i<=Length[list], i++, M[[Sequence @@ list[[i]] = values[[i]]]
would be an easy solution. $\endgroup$M = Normal@SparseArray[Thread[list -> values], {3, 3}]
. $\endgroup$##
? To passlist
andvalues
like inM[[1, 1]] = a
. I attemptedM[[##]] & /@ list = values
but failed. $\endgroup$