5 views (last 30 days)

Hello everyone,

I have an optimization variable

y=optimvar('y',[4,1],'Type','integer','LowerBound',0,'UpperBound',1);

and I want to put this "y" into a matrix which have 4 time periods, so i created a multidimensional matrix as this:

yi=y([ 1 2 3 4; 2 2 3 4;3 3 3 4;4 4 4 4]);

yi(:,:,2)= y([ 1 2 3 4; 2 2 3 4;3 3 3 4;4 4 4 4]);

yi(:,:,3)= y([ 1 2 3 4; 2 2 3 4;3 3 3 4;4 4 4 4]);

yi(:,:,4)= y([ 1 2 3 4; 2 2 3 4;3 3 3 4;4 4 4 4]);

where the third dimension is about time, which I have 4 years in my problem. While I run this code in matlab it says this is an illegal assignment, could anyone help me with that?

Thank you!

Kavya Vuriti
on 24 Jul 2019

Hi,

From the question I understand that you created an optimization variable y and wants to create a 3-dimensional matrix yi in a specific layout where the 3rd dimension is time.

Try using the following code:

y=optimvar('y',[4,1,4],'Type','integer','LowerBound',0,'UpperBound',1);

yi=[y(1,1,1),y(2,1,1),y(3,1,1),y(4,1,1);y(2,1,1),y(2,1,1),y(3,1,1),y(4,1,1);y(3,1,1),y(3,1,1),y(3,1,1),y(4,1,1);y(4,1,1),y(4,1,1),y(4,1,1),y(4,1,1)];

yi(:,:,2)=[y(1,1,2),y(2,1,2),y(3,1,2),y(4,1,2);y(2,1,2),y(2,1,2),y(3,1,2),y(4,1,2);y(3,1,2),y(3,1,2),y(3,1,2),y(4,1,2);y(4,1,2),y(4,1,2),y(4,1,2),y(4,1,2)];

yi(:,:,3)=[y(1,1,3),y(2,1,3),y(3,1,3),y(4,1,3);y(2,1,3),y(2,1,3),y(3,1,3),y(4,1,3);y(3,1,3),y(3,1,3),y(3,1,3),y(4,1,3);y(4,1,3),y(4,1,3),y(4,1,3),y(4,1,3)];

yi(:,:,4)=[y(1,1,4),y(2,1,4),y(3,1,4),y(4,1,4);y(2,1,4),y(2,1,4),y(3,1,4),y(4,1,4);y(3,1,4),y(3,1,4),y(3,1,4),y(4,1,4);y(4,1,4),y(4,1,4),y(4,1,4),y(4,1,4)];

Hope it works.

For more information on creating multi-dimensional binary optimization variables, you can refer the following link: https://www.mathworks.com/help/optim/ug/optimvar.html#mw_cedf0526-03c6-46b9-aba3-a694d89d1003

Opportunities for recent engineering grads.

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

Start Hunting!
## 4 Comments

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/472644-multidimensional-matrix-optimization-error#comment_726571

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/472644-multidimensional-matrix-optimization-error#comment_726571

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/472644-multidimensional-matrix-optimization-error#comment_726573

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/472644-multidimensional-matrix-optimization-error#comment_726573

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/472644-multidimensional-matrix-optimization-error#comment_726664

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/472644-multidimensional-matrix-optimization-error#comment_726664

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/472644-multidimensional-matrix-optimization-error#comment_726752

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/472644-multidimensional-matrix-optimization-error#comment_726752

Sign in to comment.