# Change elements on different positions within a list

How can I change the elements of a list, say on positions {{1, 4}, {3, 1}, {2, 3}, {4, 4}} in one go?

Take this list for example:

In:= (m = Array[RandomInteger[9] &, {4, 4}]) // TableForm

Out= 0  6   2   8
3  8   8   0
3  7   4   1
4  8   2   4


This is how I want to the elements on the specified positions to change.

m[[1, 4]] = Style[m[[1, 4]], 14, Bold, Blue];

m // TableForm


The result is that the 8 in the last element of the first row is now blue and bold (and bigger to stand out).

-

MapAt is probably the most direct:

MapAt[Style[#, Bold, Blue] &, m, {{1, 4}, {3, 1}, {2, 3}, {4, 4}}] // TableForm


However this makes a copy of the table rather that modifying it in place. You could use m = MapAt[ . . . ] but this still uses additional memory.

Although it may not meet the requirement of "in one go" doing your Part assignment for each element is I believe the most efficient method:

(m[[##]] = Style[m[[##]], Bold, Blue]) & @@@
{{1, 4}, {3, 1}, {2, 3}, {4, 4}};

m // TableForm


J. M. remarks that this method would be better implemented using Scan. That is true if you are doing many replacements as the list built by using @@@ takes memory. With Scan it would look like this:

(m[[##]] = Style[m[[##]], Bold, Blue]) & @@ # & ~Scan~
{{1, 4}, {3, 1}, {2, 3}, {4, 4}};

-
Thanks, I guess both approaches can be used in many situations. – sjdh Jun 30 '12 at 9:03
How did you make the pictures of the matrix? – sjdh Jun 30 '12 at 9:04
I'd have used Scan[] myself instead of Apply[] for the second method... – J. M. Jun 30 '12 at 13:06
@paradox2 I understand now. I don't believe it can be done for the "pixel perfect Windows version" because that uses Copy As Bitmap which doesn't support transparency. For the "multiplatform" version (which can also be used on Windows) this should be simply a matter of inserting Background -> None into Rasterize[target, "Image"]; and ExportString[g, "PNG"];. However, you should contact Szabolcs to make the changes as I am not deeply familiar with his code. "Upload SE transparent" would be a useful button in my opinion. – Mr.Wizard Jul 2 '12 at 14:20