Tuesday, March 26, 2013

Exporting a Matlab matrix to GAMS

There are (at least) two ways to export data from Matlab and loading them into GAMS: using a CSV file or using a GDX file. Here is a small comparison with a large matrix (4 million entries):

 

CSV file

GDX file

Matlab

m=2000;

n=2000;

A=rand(m,n);


B=[0,1:n; (1:m)' A];


csvwrite(
'c:\projects\tmp2\a.csv',B);

m=2000;

n=2000;

A=rand(m,n);

 

p.name = 'A';

p.type = 'parameter';

p.val = A;

p.form = 'full';

 

path('c:\gams\win64\24.0',path);

wgdx ('c:\projects\tmp2\data.gdx', p);

 

 

Time

3.85 seconds

4.2 seconds

GAMS

table a(*,*)
$offlisting
$ondelim
$include a.csv
$offdelim
$onlisting

parameter a(*,*);
$gdxin data.gdx
$load a


Time

0.72 seconds

0.69 seconds

The timings are similar. As always the timings need to be taken with a grain of salt.

2 comments:

  1. Hi, I am a new user of cplex.
    Why I add a line "Display a.L;" when I use the gdx file code, there is some error?

    ReplyDelete
  2. Hello, I am trying to modify a table in gams from matlab. for example, in gams I set up :

    Table data(s,t,r)
    r1 r2
    s1.t1 1.1 2.3
    s1.t2 5.4 3.6

    s2.t1 4.2 5.2
    s2.t2 1.3 0.7

    How can I change this table within matlab? thanks.

    ReplyDelete