I am trying to write a function that will find a sum of all diagonal elements of matrix. I don't know how to do it correctly. This is how I tried do do it.
matrix[n_, {rov_, column_}] := RandomInteger[n, {rov, column}]
matrix[10, {5, 5}]
For[sum = 0; i = 0, i < rov, i++;
For[j = 0, j < column, j ++;
If[i == j, sum = sum + matrix[10, {5, 5}]
]
]
]
First I have written a function that generates matrix. And than I tried to write the function that will find the sum of diagonal elements.

Tr[A]does it all. – David G. Stork Mar 13 '18 at 20:54Tr, you could at least useTotal @* Diagonal. – Patrick Stevens Mar 13 '18 at 21:29