Skip to main content
1 of 5
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

Leonid provides a nice method for doing this within "pure functions" but I think it should be pointed out that the common method for doing this is pattern matching.

For example:

f[a_, {i_, j_}] := a == 0 || Abs[i - j] <= 1 triDiagonalQ[mat_] := And @@ Flatten @ MapIndexed[f, mat, {2}] 

Or:

triDiagonalQ[mat_] := And @@ Flatten @ MapIndexed[#2 /. {i_, j_} :> # == 0 || Abs[i - j] <= 1 &, mat, {2}] 
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k