Hello I need to sum the elements of the two main diagonal elements of a 9x9 matrix. The first one is easy Sum(i,x(i,i)) but for the second I tried sum(i,x(i,9-i)) or analogous variations but I didn't succeed.

Thanks for your help

Pierluigi

## Sum Counterdiagonal elements

### Re: Sum Counterdiagonal elements

With lag and leads (see https://www.gams.com/latest/docs/UG_Ord ... dOperators ) you need to have i+(expr) or i-(expr) (-- or ++) also work. So your 9-i is equivalent to i-i+9-i = i+(9-2*i). Here is the complete GAMS program:

-Michael

Code: Select all

```
set i /1*9/; alias (i,j);
parameter x(i,j); x(i,j) = uniformInt(0,10);
scalar diag1, diag2;
diag1 = sum(i, x(i,i));
diag2 = sum(i, x(i,i+(card(i)-2*ord(i))));
option x:0:1:1; display x, diag1, diag2;
```

### Re: Sum Counterdiagonal elements

Thanks, very helpful. I realised that searching for reverse order I could have found some hints and I came upwith

SUM(i, x(i,i+(10-2*ord(i))))

thanks again

SUM(i, x(i,i+(10-2*ord(i))))

thanks again