## Sum Counterdiagonal elements

Problems with syntax of GAMS
pzezza
User
Posts: 3
Joined: 11 months ago

### Sum Counterdiagonal elements

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.
Pierluigi

bussieck
Moderator
Posts: 208
Joined: 2 years ago

### 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:

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;
``````
-Michael

pzezza
User
Posts: 3
Joined: 11 months ago

### 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