Sum Counterdiagonal elements

Problems with syntax of GAMS
Post Reply
pzezza
User
User
Posts: 3
Joined: 9 months ago

Sum Counterdiagonal elements

Post by pzezza » 9 months ago

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

User avatar
bussieck
Moderator
Moderator
Posts: 141
Joined: 1 year ago

Re: Sum Counterdiagonal elements

Post by bussieck » 9 months ago

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
User
Posts: 3
Joined: 9 months ago

Re: Sum Counterdiagonal elements

Post by pzezza » 9 months ago

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

Post Reply