Matrix dimensions must agree error


(Mohan Lal) #1

Hi guys,
I’m trying to impose an inequality constraint in my MILP problem to be solved in MATLAB using CVX, it looks like;


P_max = a; % a is any constant
P_min = -P_max;
.
.
.
cvx_begin
cvx_solver MOSEK
% define variables
variables P(T,U) . . . . . ;
variable b(T,1) binary;
% objective fun
minimize (----- obj fn ----);
% constraints
subject to
.
.
.
.
P_min .*(1-b) <= P <= P_max .*b;
%
cvx_end

However, I was getting the error “Matrix dimensions must agree” as the dimensions in the inequality weren’t same to multiply element-wise. So I used “gmultiply” command (https://www.mathworks.com/help/deeplearning/ref/gmultiply.html), but CVX doesn’t seems supporting it and this time gave the error “Cannot combine values of class double with cvx”
Where I’m getting it wrong??
This might seem very simple query for you guys, but I’ve been trying to fix it since 4-5 days but couldn’t succeed. Please suggest the solution.

Thanks,
Mohan


(Mark L. Stone) #2

P_min .*(1-b) and `P_max .*b are both T by 1 vectors, but P is a T by U matrix. That is why the dimensions are incompatible.

The needed code depends on what you want. If you want to impose the same inequalities for each column of U, you could use
repmat(P_min*(1-b),1,U) <= P <= repmat(P_max*b,1,U)`

Note that * does the same thing as.* in your example.


(Mohan Lal) #3

Thank you Mark. That’s exactly what I was looking for. I wanted to impose same inequalities for each column of U, I couldn’t explain my question well, but you got my point.