0
$\begingroup$

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

$\endgroup$
  • 2
    $\begingroup$ Can't you just do sparse[[i,j]]=newval...? $\endgroup$ – Marius Ladegård Meyer Jan 26 '16 at 5:54
  • $\begingroup$ @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. $\endgroup$ – TeeJay Jan 26 '16 at 6:00
  • 3
    $\begingroup$ I'm not sure I follow. Could you add a minimal working example to your question? $\endgroup$ – Marius Ladegård Meyer Jan 26 '16 at 6:52
  • $\begingroup$ @TeeJay, Something like Normal@KeyTake[atest, {{1, 1}, {2, 2}}]? for {1,1} and {2,2} elements as axample? $\endgroup$ – garej Jan 26 '16 at 19:07
  • $\begingroup$ arrayrules /. ({i, j} -> _) :> {i, j} -> newval or arrayrules /. (idx -> _) :> idx -> newval, where idx is the {i,j} pair? $\endgroup$ – Michael E2 Jan 28 '16 at 12:21
3
$\begingroup$

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}

| improve this answer | |
$\endgroup$
2
$\begingroup$
(* 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}}

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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