MATLAB Answers

How to make a matrix compose by seperate optimization variables

5 views (last 30 days)
BOWEN LI on 2 Jul 2019
Edited: Matt J on 3 Jul 2019
Hi everyone,
I am thinking if it possible to put several optimizaiton variables into a matrix form. My optimization variable is defined by this syntax:
>> z1=optimvar('z1','Type','integer','LowerBound',0,'UpperBound',1)
>> z2=optimvar('z2','Type','integer','LowerBound',0,'UpperBound',1)
>> z3=optimvar('z3','Type','integer','LowerBound',0,'UpperBound',1)
It turns out that my 'zij' is a set of arrays not matrix. Anyone has idea about that? Thanks so much!


Sign in to comment.

Accepted Answer

Matt J
Matt J on 2 Jul 2019
This works fine, once you remove the quotes
>> zij=[z1,z2,z3; z2,z2,z3; z3,z3,z3];
>> showexpr(zij)
(1, 1)
(2, 1)
(3, 1)
(1, 2)
(2, 2)
(3, 2)
(1, 3)
(2, 3)
(3, 3)


BOWEN LI on 3 Jul 2019
Thank you so much. May I have one more question that if the matrix works, I am wondering if the matrix can be used as a optmization variable like:
Here, zij is a optimization variable, and each element of zij is optimization variable as well.
Matt J
Matt J on 3 Jul 2019
If all the matrix elements Zmatrix(i,j) were independent variables, you could do so as follows,
However, in your posted example, the Zmatrix(i,j) were not all independent of each other.
You can create a "dependent optimization variable" for your posted example like this,
Zmatrix=z([ 1 2 3; 2,2,3;3 3 3]);
So, note that this Zmatrix object is of type OptimizationVariable,
Zmatrix =
3×3 OptimizationVariable array with properties:
Read-only array-wide properties:
Name: 'z'
Type: 'integer'
IndexNames: {{} {}}
Elementwise properties:
LowerBound: [3×3 double]
UpperBound: [3×3 double]
Reference to a subset of OptimizationVariable with Name 'z'.
however, it is really just a matrix of pointers, basically, to different z(i). It has no independence existence of its own. In particular, if you modify Z(1,2).LowerBound, then Z(2,1).LowerBound and Z(2,2).LowerBound will change as well, because we defined all three to be aliases of the same independent variable z(2). For additional reading,
BOWEN LI on 3 Jul 2019
Hi, I think the dependent optimization variable you said "Zmatrix" is just what I want.
Because my goal is to let Z(1,2) and Z(2,1) have the same upperbound and lowerbound while Z(1,2) and Z(2,1) reference to the same independent variable z(2). So in this case, I can multiply 'Zmatrix' with the other matrix which I also index as [1 2 3; 2,2,3;3 3 3].
So, in this sense, Z(1,3),Z(3,1),Z(3,2), and Z(3,3) all reference to z(3) right?
Thank you so much!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!