How to extract part of a table data and assign it to another table parameter Topic is solved

Problems with syntax of GAMS
Post Reply
cardyn
User
User
Posts: 3
Joined: 2 months ago

How to extract part of a table data and assign it to another table parameter

Post by cardyn » 2 months ago

Hi everyone, I am new to GAMS. I got a problem about assignment in GAMS.
I have a table data X(rows,cols), how can I extract part of it (e.g. the first 10 rows) and assign it to another table parameter Y(subr,cols)? For example:

Sets
rows /r1*r100/
cols /c1*c4/
subr /sr1*sr10/;

Table X(rows,cols)
c1 c2 c3 c4
r1 1 2 3 4
r2 5 6 7 8
r3 9 10 11 12
..

Parameter Y(subr,cols);

User avatar
Renger
Posts: 395
Joined: 3 years ago

Re: How to extract part of a table data and assign it to another table parameter

Post by Renger » 2 months ago

Hi Cardyn
Here are two options for this

Code: Select all

Parameter Y(rows,cols);

y(rows,cols)$(ord(rows) lt 11) =  X(rows,cols);
However, in this case, the table will contain zeros for the rest of the rows (as they are not initialized.
You can use a subset of rows (and not a new set):

Code: Select all

set subr(rows) /r1*r10/;

Parameter Y(rows,cols);

y(srows,cols) = x(srows, cols);
Hope this helps

Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

cardyn
User
User
Posts: 3
Joined: 2 months ago

Re: How to extract part of a table data and assign it to another table parameter

Post by cardyn » 2 months ago

Hi Renger,

Thank you very much for your excellent answers.
Here I have another solution after I worked for several hours on this problem. Hope it can also help in case someone need it.

Code: Select all

Parameter X(rows,cols);
Parameter Y(subr,cols);
loop((rows,subr)$ [ord(rows)=ord(subr)],
Y(subr,cols)=X(rows, cols);
);
Kind regards
Renger wrote:
2 months ago
Hi Cardyn
Here are two options for this

Code: Select all

Parameter Y(rows,cols);

y(rows,cols)$(ord(rows) lt 11) =  X(rows,cols);
However, in this case, the table will contain zeros for the rest of the rows (as they are not initialized.
You can use a subset of rows (and not a new set):

Code: Select all

set subr(rows) /r1*r10/;

Parameter Y(rows,cols);

y(srows,cols) = x(srows, cols);
Hope this helps

Cheers
Renger

Post Reply