Page 1 of 1
How to extract part of a table data and assign it to another table parameter
Posted: Tue Jul 23, 2019 11:04 am
by cardyn
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);
Re: How to extract part of a table data and assign it to another table parameter
Posted: Wed Jul 24, 2019 7:59 am
by Renger
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
Re: How to extract part of a table data and assign it to another table parameter
Posted: Wed Jul 24, 2019 10:48 am
by cardyn
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: ↑4 years 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