# Referencing elements of ArrayRules

How do I call elements of ArrayRules by their indices rather than by Part[ArrayRules[list],i]? I'd like to call the element that looks like {i, j}-> valby referring to the pair {i, j} rather than by a single numerical index denoting the location within the list. The reason for this is I would like to create a new SparseArray based on the elements of a list, but I can't figure out how to change val in ArraryRules while preserving the rules that map it to a SparseArray.

I'd like to call a specific element {i, j} and assign the rules {i, j}-> newval

• Can't you just do sparse[[i,j]]=newval...? – Marius Ladegård Meyer Jan 26 '16 at 5:54
• @MariusLadegårdMeyer Not for what I'm doing. I'm taking a subset of a list and modifying it and then replacing that part of the original list, so I need to be able to pull individual elements by their indices. – TeeJay Jan 26 '16 at 6:00
• I'm not sure I follow. Could you add a minimal working example to your question? – Marius Ladegård Meyer Jan 26 '16 at 6:52
• @TeeJay, Something like Normal@KeyTake[atest, {{1, 1}, {2, 2}}]? for {1,1} and {2,2} elements as axample? – garej Jan 26 '16 at 19:07
• arrayrules /. ({i, j} -> _) :> {i, j} -> newval or arrayrules /. (idx -> _) :> idx -> newval, where idx is the {i,j} pair? – Michael E2 Jan 28 '16 at 12:21

Are you aware that you can define a Rule to specify a change of Rules ?

For example here is a replacement of the value indexed by {1,1} :

{{1, 1} -> 2, {1, 2} -> 3} /.  ({1, 1} -> _) -> ({1, 1} -> XXX)


{{1, 1} -> XXX, {1, 2} -> 3}

(* make a dummy example array *)
test = RandomInteger[10, {5, 5}];

(* get rules *)
atest = ArrayRules[test];

(* Prefix with our replacements - the default behavior *)
(* for sparse array is to ignore subsequent duplicate positions *)

patest = SparseArray[Join[{{1, 1} -> -1, {2, 2} -> -2}, atest]];

(* show result *)
test
patest // Normal


{{1,2,8,2,10},{10,8,6,7,10},{3,1,7,5,8},{8,8,3,0,0},{6,10,4,4,0}}

{{-1,2,8,2,10},{10,-2,6,7,10},{3,1,7,5,8},{8,8,3,0,0},{6,10,4,4,0}}