I want to assign new values to the anti-diagonal elements of a square matrix m
with the values of its diagonal elements. For that, I want to make use of ReplacePart
, with the fact that
Part position specifications can be patterns
as stated in the documentation.
I want to use a command like this:
ReplacePart[m, (*pattern goes here*) :> Diagonal[m]〚i〛]
I need help with writing a pattern to access the anti-diagonal elements.
They have the property of i+j=Length[m]+1
but I can't find a way to implement it into a pattern match.
For example, with
m={
{100, 9, 3, 8, 0},
{4, 200, 6, 4, 9},
{5, 2, 300, 7, 8},
{7, 9, 6, 400, 5},
{6, 1, 4, 2, 500}
}
I want to produce the matrix
{
{100, 9, 3, 8, 100},
{4, 200, 6, 200, 9},
{5, 2, 300, 7, 8},
{7, 400, 6, 400, 5},
{500, 1, 4, 2, 500}
}
{i_, j_} /; j == Dimensions[m][[2]] + 1 - i :> Diagonal[m][[i]]
? $\endgroup$