# How to write if statement and for loop? [closed]

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.

## closed as off-topic by m_goldberg, Henrik Schumacher, José Antonio Díaz Navas, LCarvalho, gwrMar 15 '18 at 8:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Henrik Schumacher, José Antonio Díaz Navas, LCarvalho, gwr
If this question can be reworded to fit the rules in the help center, please edit the question.

• Yep: Tr[A] does it all. – David G. Stork Mar 13 '18 at 20:54
• Thx :). But if you know how to write a code I would be thankful – Cro Simpson2.0 Mar 13 '18 at 20:58
• your code as written generates a new random matrix for each iteration of the loop. Is that what you want? – george2079 Mar 13 '18 at 21:10
• Your main problem (as written) is you have semicolons instead of commas after your increment in the for loop. – wxffles Mar 13 '18 at 21:13
• obligatory link mathematica.stackexchange.com/q/134609/2079 – george2079 Mar 14 '18 at 1:50

Let's have an answer so this question can be ticked off ...

To calculate the sum of the elements on the main diagonal of a matrix use the intrinsic function Tr. In your case something like

Tr[matrix[10, {5, 5}]]


Before you use For, read the material @george2079 pointed you at. If you are a beginner with Mathematica check out the documentation for Tr and follow the links to other related functions, do some discovery for yourself of what is already built in to the system.

In the code you've shown us you've made some slips in using ; and , correctly in the If and For calls. For someone whose mind has been over-influenced by C and its ilk the two punctuation marks might, on studying the Mathematica documentation, seem to be used the wrong way round.

As @george2079 has also pointed out your matrix is a function which, each time it is called, generates a new random matrix. It doesn't make any sense to call it at each execution of the If statement inside the loop nest. Perhaps you should write

mat1 = matrix[10, {5, 5}]


and then execute

Tr[mat1]

(matrix = Array[a, {5, 5}]) // MatrixForm Tr[matrix]

(* a[1, 1] + a[2, 2] + a[3, 3] + a[4, 4] + a[5, 5] *)

Total@Diagonal@matrix

(* a[1, 1] + a[2, 2] + a[3, 3] + a[4, 4] + a[5, 5] *)

Total[matrix[[#, #]] & /@ Range]

(* a[1, 1] + a[2, 2] + a[3, 3] + a[4, 4] + a[5, 5] *)

Total@Table[matrix[[i, i]], {i, 5}]

(* a[1, 1] + a[2, 2] + a[3, 3] + a[4, 4] + a[5, 5] *)

For[sum = 0; i = 1, i < 6, i++, sum += matrix[[i, i]]]; sum

(* a[1, 1] + a[2, 2] + a[3, 3] + a[4, 4] + a[5, 5] *)