Size of a Table

Problems with syntax of GAMS
Post Reply
MTSaba
User
User
Posts: 2
Joined: 4 years ago

Size of a Table

Post by MTSaba »

Hello,
is there a GAMS command that will return the number of rows of a table.

Please help.

MTSaba
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Size of a Table

Post by Renger »

Hi
No, there is no command for that, however, you can find it out using some code:

If the table is defined over known sets, you can do something like this:

Code: Select all

set i /i1*i4/, j /j1*j5/;

parameter a(i,j);

a(i,j) = uniform(0,1);


parameter dimension;

dimension("a","row") = sum(i,1);
dimension("a","col") = sum(j,1);

display dimension;
If your table is not defined over sets, but is read from excel and saved in a gdx file, you can use the load function to gather the row and column labels as a set (see example 4 of the gdx documentation). You are than back again at the code above.
Let's say you read the following table from excel:

Code: Select all

   a  b  c  d
x  0  2  4  6
y  -1 3  0  4
You could do the following (after reading the table from the excel sheet with gdxxrw):

Code: Select all

set rowstest, colstest;
parameter 
    test(rowstest, colstest)  The table,
    dimension;

$gdxin data
$load rowstest<-data.dim1 colstest<-datadim2 test

dimension("test","row") = sum(rowstest,1);
dimension("test","col") = sum(colstest,1);
I hope this helps
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
MTSaba
User
User
Posts: 2
Joined: 4 years ago

Re: Size of a Table

Post by MTSaba »

thanks alot appreciated,
it is related to the second case of table is not defined over sets, but it is read from excel where I'm trying to create dynamic sets from imported tables from excel.
Post Reply