# Compilable table of Matrix diagonals function

1,
I am trying to make Compilable function which takes Matrix of zeroes and ones f.e:

R= {{1, 1, 1, 0, 0, 0, 1, 1, 1, 0}, {1, 1, 0, 0, 0, 0, 1, 1, 1, 0}, {1,
0, 1, 0, 0, 1, 1, 0, 1, 1}, {0, 0, 0, 1, 1, 1, 0, 0, 0, 1}, {0, 0,
0, 1, 1, 1, 0, 0, 0, 1}, {0, 0, 1, 1, 1, 1, 0, 0, 1, 1}, {1, 1, 1,
0, 0, 0, 1, 1, 1, 0}, {1, 1, 0, 0, 0, 0, 1, 1, 1, 0}, {1, 1, 1, 0,
0, 1, 1, 1, 1, 0}, {0, 0, 1, 1, 1, 1, 0, 0, 0, 1}}


and will make a table of its diagonals, but the problem:CompiledFunction::cflist: Nontensor object generated; proceeding with uncompiled evaluation. occures. My function is:

f3 = Compile[{{R, _Real, 2}},
Table[Flatten[{0, Table[R[[i]][[i + j]], {i, 1, Length[R] - j}],
0}], {j, 1, Length[R] - 1}], CompilationTarget -> "C",
RuntimeAttributes -> Listable, RuntimeOptions -> "Speed"]


Any suggestion how can I debug it please ?

2, Then I want to make again Compilable fction which can determinate from Flatten[f3[R]] the number of ones without neighbour, I mean, f.e. vector {1,0,1,0,1,1} has a 2 ones without neighbour.

Or even better Compilable function which can determinate the number of all the sequances of 1 11 111 1111 ... in Flatten[f3[R]].

• A note: if the result of a function's evaluation is a "ragged array", such as a table of a matrix's diagonals, you can't compile. In any case: Table[ArrayPad[Diagonal[R, k], 1], {k, Length[R] - 1}] Apr 8, 2018 at 15:48

Table[ArrayPad[Diagonal[R, k], 1], {k, Length[R] - 1}]

to create your diagonals. As for your second question, this can also be done with built-in list manipulation. Take the flattened list of diagonals and split them up. Then, you need to delete the elements containing zeroes and Tally does the rest
diag = Flatten@Table[ArrayPad[Diagonal[R, k], 1], {k, Length[R] - 1}];