Reputation: 298
If I have a set and parameter
Set A/a1,a9/;
Parameter T(A);
*Suppose p is T(A)=121311332
T(A) /1 1,2 2,3 1,4 3,5 1,6 1,7 3,8 3,9 2/;
How to find the third one and changed it to 4?
(E.g. 121311332 changed to 121341332)
Generally How may I get the nth repetitive element (1,2or 3) from set A and change it?Is there anyway to do that?
Upvotes: 0
Views: 92
Reputation: 2292
Edit after clarification in comments:
To change the third occurrence of 1 to 4 you can do this:
Set A /a1*a9/;
Parameter T(A) /a1 1,a2 2,a3 1,a4 3,a5 1,a6 1,a7 3,a8 3,a9 2/;
Scalar oneCnt / 0 /;
Display T;
loop(A,
if(T(A)=1,
oneCnt = oneCnt+1;
if(oneCnt=3,
T(A)=4;
break;
)
);
);
Display T;
Originally, I thought that the third element should be changed. That could be done like this:
You can use the ord operator (https://www.gams.com/latest/docs/UG_OrderedSets.html#UG_OrderedSets_TheOrdOperator) to do that:
Set A/a1*a9/;
Parameter T(A) /a1 1,a2 2,a3 1,a4 3,a5 1,a6 1,a7 3,a8 3,a9 2/;
Display T;
T(A)$(ord(a)=3) = 4;
Display T;
Upvotes: 1