Page 1 of 1

Sum Counterdiagonal elements

Posted: Thu Dec 21, 2017 9:16 am
by pzezza
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

Re: Sum Counterdiagonal elements

Posted: Thu Dec 21, 2017 10:09 am
by bussieck
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

Re: Sum Counterdiagonal elements

Posted: Thu Dec 21, 2017 2:00 pm
by pzezza
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