Assign the same variable to all elements of a Matrix

19 visualizaciones (últimos 30 días)
Adil Bendouro
Adil Bendouro el 8 de Nov. de 2021
Comentada: Adil Bendouro el 9 de Nov. de 2021
I would like to assign a variable to all elements of a Matrix without using a for loop to obtain a matrix M such that : M(nxn) = [x , x, x , x ,x ... ].
In my particular case x is an sdpvar and M is a matrix I use to set up the constraints of an optimisation problem.
Thank you

Respuestas (2)

Matt J
Matt J el 8 de Nov. de 2021
I haven't worked with YALMIP, but can't you do
M=sdpvar(n);
M(:)=x;
  1 comentario
Adil Bendouro
Adil Bendouro el 8 de Nov. de 2021
That is what I initially tried to do but it doesn't seem to work when the variables are multidimensional.
In my case I have:
M = sdpvar(10,10,10);
x = sdpvar(10,1);
M(:,:,:) = x;
which gives: In an assignment A(I) = B, the number of elements in B and I must be the same.

Iniciar sesión para comentar.


John D'Errico
John D'Errico el 8 de Nov. de 2021
As with Matt, I have not used the code, but this should work.
M = repmat(sdpvar(1,1),[10 10 10]);
  1 comentario
Adil Bendouro
Adil Bendouro el 9 de Nov. de 2021
Unfortunately repmat didn't work for me. I believe that it is because it assigns copies of the variable x instead of the actual variable. Therefore the constraints of my linear program are not applied to x, which makes the optimisation problem unbounded.

Iniciar sesión para comentar.

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by