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!
outends up being{{{}, {}, 4}, {{}, 4, 5}, {{}, {}, {}}}. Also you can useScanrather thanMaphere – acl Oct 26 '12 at 17:31Scan. – Van Oct 26 '12 at 21:51