Getting the index position of the largest element of a parameter

Frequently asked questions about GAMS

Moderator: aileen

Forum rules
Please ask questions in the other sub-forums
Locked
aileen
User
User
Posts: 136
Joined: 4 years ago

Getting the index position of the largest element of a parameter

Post by aileen »

I have a problem of choosing indexes for the maximum value of a parameter. Let a(i,j) be a parameter with sets i /i1*i5/, j /j1*j10/. Let

Code: Select all

Maxa = smax((i,j), a(i,j));
How do I get the index for i and j which lead to value Maxa?
aileen
User
User
Posts: 136
Joined: 4 years ago

Re: Getting the index position of the largest element of a parameter

Post by aileen »

Try:

Code: Select all

Set i /i1 * i5/,
    j /j1 * j4/,
    posmax(i,j) 'position of largest element';
     
Parameter a(i,j) 'some data',
          maxa   'largest element of a';

a(i,j)=uniformint (1,100);
maxa = smax((i,j), a(i,j));
posmax(i,j) = a(i,j) = maxa;

Option decimals=0;
display a, maxa, posmax;

Code: Select all

----     13 PARAMETER a  some data

            j1          j2          j3          j4

i1          18          85          56          31
i2          30          23          35          86
i3           7          51         100          58
i4         100          77          14          64
i5          16          26          67          44


----     13 PARAMETER maxa                 =          100  largest element of a

----     13 SET posmax  position of largest element

            j1          j3

i3                     YES
i4         YES
In case just one element is wanted and not all index positions with the maximum number, one can use a Singleton Set like this:

Code: Select all

Singleton Set posmaxS(i,j);
Option strictSingleton=0;
posmaxS(i,j) = a(i,j) = maxa;
display posmaxS;

Code: Select all

----     18 SET posmaxS  

            j3

i3         YES
Locked