Hi, I'm new with GAMS and I'm struggling with a very simple problem.
I wish to define the product between a matrix(k,m) and a vector(k), e.g.:
A = [0.082 0.036 ; 0.036 0.127] and v = [1 ; 0]
Multiplying A*v I expect to obtain the first column of A, so: x = A*v = [0.082; 0.036]
If I try:
x(k) = sum(m,A(k,m)*v(k))
My result is a single element that is the sum of the two elements I expect to be in the column, hence 0.118.
If I try with the product (x(k) = prod(...)), my result is a single element that is the product of the two elements I expect to be in the column, hence 0.003.
What am I doing wrong? Thanks a lot to anyone will help.
Product between matrices and vector

 User
 Posts: 53
 Joined: 7 months ago
 Location: Dallas, TX, USA
Re: Product between matrices and vector
Hi,
Try to use loops. I wrote a simple code for you.
If you need to assign the matrix and vector by index, just add one more index to vector and matrix and one more outer loop.
Cheers!
Gabriel
Try to use loops. I wrote a simple code for you.
Code: Select all
set col /1*4/
set row /1*5/
Table matrix(row, col)
1 2 3 4
1 2 3 4 5
2 4 6 2 1
3 9 7 1 0
4 6 7 1 8
5 7 2 3 1
;
Parameter vector(col)/
1 1
2 1
3 0
4 0
/;
Parameter result(row);
loop(row,
result(row) = sum(col, matrix(row, col) * vector(col));
);
display result;
Cheers!
Gabriel
Re: Product between matrices and vector
Hi Gabriel, many thanks!