Filter löschen
Filter löschen

YALMIP constraint - All values should be equal

11 Ansichten (letzte 30 Tage)
SB
SB am 7 Feb. 2015
Kommentiert: SB am 8 Feb. 2015
I am solving a optimization problem with different constraints and objectives using YALMIP. One of the constraints should be the following: The entries in the second row of my 3D matrix should have the same values in every third dimension. At the moment I am doing this with a for loop in the following way:
P = sdpvar(122,7,8);
Constraints = [];
for i = 2:8
Constraints = [Constraints, P(2,:,1) == P(2,:,i)];
end
So in other words this is what I want: P(2,1,1) should be the same value as P(2,1,2) and P(2,1,3) and ... and P(2,1,8). Moreover P(2,2,1) should be the same value as P(2,2,2) and P(2,2,3) and ... and P(2,2,8). And so on. But P(2,1,1) does not have to be equal to P(2,2,1) for example!
I am quite sure that the code stated above applies the constraint correctly. Is there however a better way of implementing this without a for-loop?
Or in a more general formulation: Is there a nice way of constraining all elements of a certain dimension to be the same value? Notice that this could even be a 2D matrix or a vector, the question remains the same.

Akzeptierte Antwort

Johan Löfberg
Johan Löfberg am 8 Feb. 2015
I believe you are implementing
diff(P(2,:,:),[],3)==
BTW, if possible, I recommend you to post YALMIP questions on the YALMIP google groups forum. More likely to get quick response
  1 Kommentar
SB
SB am 8 Feb. 2015
Yes I think that's what I was looking for! So if I understand your answer correctly, the complete constraint formulation is the following:
P = sdpvar(122,7,8);
Constraints = [];
Constraints = [Constraints, diff(P(2,:,:),1,3) == 0];

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by