# Matrix of variables

**URL:** <https://discuss.ampl.com/t/matrix-of-variables/1084>\
**Category:** Support\
**Created:** [May 3, 2024, 4:26pm UTC](https://discuss.ampl.com/t/matrix-of-variables/1084 "2024-05-03T16:26:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ae.naderi](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@ae.naderi](https://discuss.ampl.com/u/ae.naderi)\
**Post date:** [May 3, 2024, 4:26pm UTC](https://discuss.ampl.com/t/matrix-of-variables/1084/1 "2024-05-03T16:26:30Z")

</div>

Hello everyone,

I have four variables like this:

```auto
var p0;
var b;
var x;
var y;

```

And I want to create a matrix of these variables with dimensions (4\*1) like so:

> `var X = [p0, b, x, y];`

Could anyone please provide me with the syntax for this matrix?Furthermore, I would like to express the following constraint:

> `A * X < B`

Where A and B are matrices of values that I have defined previously and are like:

> param B :=  
> 1 50  
> 2 140  
> 3 9  
> 4 -1  
> 5 35.77  
> 6 -35.72  
> 7 51.38  
> 8 -51.32;

Thank you.

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [May 4, 2024, 12:16pm UTC](https://discuss.ampl.com/t/matrix-of-variables/1084/2 "2024-05-04T12:16:18Z")

</div>

Here is an example of how you could formulate an AMPL model for the situation that you describe:

```auto
param m; # number of constraints
set V; # set of variables

param A {1..m, V};
param B {1..m};

var X {V};

subject to constr {i in 1..m}:
   sum {j in V} A[i,j] * X[j] <= B[i];

```

Then in the data, you can specify

```auto
set V := p1 b x y ;

```

Alternatively, if the variables will always be the same, you can just write this set definition in your model:

```auto
set V = {"p1","b","x","y"};

```

(Note that, in large-scale applications of linear optimization, almost all of the constraint coefficients are zero, and so it is not practical to specify coefficient matrices explicitly. For this reason, AMPL does not have a “matrix” data type.)

---

<div class="post-metadata">

**Author:** ![ae.naderi](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@ae.naderi](https://discuss.ampl.com/u/ae.naderi)\
**Post date:** [May 5, 2024, 7:18am UTC](https://discuss.ampl.com/t/matrix-of-variables/1084/3 "2024-05-05T07:18:33Z")

</div>

thanks for your response. I put your solution but the problem is not solved. I better explain once more.  
I have three matrices. that the matrix A is a 4x8 matrix, the values inside of which are the numbers that I set in the .dat file. The next matrix is B with dimensions 4x1, which is also equal to the values that I set in the .dat file. But I have a matrix named X with dimensions 1\*4. which are actually the variables that the objective function should estimate, whose names are: p0, beta, x, y.  
And my objective function is:

```auto
var p; 
var b;
var xt;
var yt;

param x{i in 1..5};
param y{i in 1..5};

param A{i in 1..8, j in 1..4};
param B{i in 1..8};

var g{i in 1..samples} = x[i] * xt *(y[i] - yt) ; 
minimize Obj_value: sum{i in 1..samples} (p + b * (g[i])^2);

```

and i want to add this constraint:  
A \*X \<= B  
for example A, B and X are:

> A[[1,1,1,1],[0,0,1,0],[1,0,0,1],…,[1,1,1,0]]  
> X [p, b, xt, yt]  
> B[3, 5,10,15]

how to write it?  
thank you

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [May 5, 2024, 1:26pm UTC](https://discuss.ampl.com/t/matrix-of-variables/1084/4 "2024-05-05T13:26:50Z")

</div>

Now that I can see your whole model, I think it may be easier for you to write the constraint like this:

```auto
subject to constr {i in 1..8}:
   A[i,1]*p + A[i,2]*b + A[i,3]*xt + A[i,4]*yt <= B[i];

```

Note that there need to be eight `B` values: `B[i]` for each `i` `in` `1..8`. Also you need to define the parameter `samples` that appears in the definitions of `g` and `Obj_value`.

If you need help specifying the data for matrix `A`, you can find some examples in section [9.3 Data in tables](https://ampl.com/wp-content/uploads/Chapter-9-Specifying-Data-AMPL-Book.pdf#page=12) in the AMPL book.

---

<div class="post-metadata">

**Author:** ![ae.naderi](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@ae.naderi](https://discuss.ampl.com/u/ae.naderi)\
**Post date:** [May 5, 2024, 5:24pm UTC](https://discuss.ampl.com/t/matrix-of-variables/1084/5 "2024-05-05T17:24:08Z")

</div>

Thank you so much! it worked.
