Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to use ReplacePart inside of Map, where the address of the element being replaced is the domain of the map, like this:

f[{i_,j_}]:=i+j;
out = ConstantArray[{}, {3,3}];
pairs = {{1,3}, {2,2}, {2,3}};
Map[(out = ReplacePart[out, #->f[#]])&, pairs]

I expect out to end up with {} in every position except out[[1,3]] == out[[2,2]] == 4 and out[[2,3]] == 5. However, out ends up with the value ReplacePart[out, 3]&. Clearly, f[] is getting evaluated the first time through, and ReplacePart is never getting evaluated. I suspect some judicious arrangement of Hold and Evaluate might solve this problem, but I can't figure it out. Can someone please help?

(For now, I've solved the problem by wrapping ReplacePart in a function and passing out and the current value of # as arguments to it, but it would be much more elegant just to do it in place.)

Thanks!

share|improve this question
here out ends up being {{{}, {}, 4}, {{}, 4, 5}, {{}, {}, {}}}. Also you can use Scan rather than Map here – acl Oct 26 '12 at 17:31
Try it in a clean kernel and confirm if you still get the same results – Rojo Oct 26 '12 at 21:18
Thanks, @Rojo, the clean kernel did it. And thanks, @acl, for pointing out Scan. – Van Oct 26 '12 at 21:51

closed as too localized by Sjoerd C. de Vries, rm -rf Oct 27 '12 at 1:03

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.