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.