3
$\begingroup$

This is a follow up on this. Based on the solution provided by @C.E. I can extract the elements around the center at any level. Now I want to replace the elements colored in red (selected).

enter image description here

So, in the shown example, the list of the selected elements is: {7, 4, 10, 6, 7, 1, 1, 1, 6, 3, 8, 0, 4, 6, 2, 7} and I wish to replace them by {6, 2, 1, 5, 0, 10, 2, 2, 3, 7, 9, 3, 3, 2, 8, 5}.

How can I do this?

$\endgroup$
3
$\begingroup$

Stuff you had:

pos[m_, {l1_, l2_}, dim_] := Position[CenterArray[
  ArrayPad[ConstantArray[1, {l1, l2}], 1], dim, 1], 0]

m = RandomInteger[9, {10, 10}];
p = pos[m, {2, 4}, {10, 10}]

and then you can do

new = Thread[p -> {6, 2, 1, 5, 0, 10, 2, 2, 3, 7, 9, 3, 3, 2, 8, 5}]
ReplacePart[m, new] // MatrixForm
$\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.