How to get all the elements in the opposite diagonal of an element

reverseDiagonal[mA_, x_] := With[{mR = Reverse@mA},
  Diagonal[mR, #] & /@ Apply[Minus@*Subtract, Position[mR, x], 2]
  ]
reverseDiagonal[{{1, 2, 3, 4}, {5, 6, 7, 7}, {9, 10, 11, 12}}, 7]
(* {{10, 7, 4}, {11, 7}} *)

You may want to map Reverse across the lists in this result.


A slightly different formulation and output from Alan's method:

fn[a_?MatrixQ, x_] :=
  a ~Reverse~ 2 /. b_ :> (Diagonal[b, #2 - #] & @@@ Position[b, x])

Test:

Mod[Range@40, 12] ~Partition~ 8 // MatrixForm

fn[%, 7]

$\begin{array}{cccccccc} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 0 & 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 & 9 & 10 & 11 & 0 \\ 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 0 & 1 & 2 & 3 & 4 \\ \end{array}$

{{7, 2, 9, 4, 11}, {5, 0, 7, 2, 9}, {0, 7, 2}}

Related:

  • Is there a built in function to obtain the back diagonal of a matrix?

Tags:

Matrix