# How to express constrain in CVX?

Sorry,I have tried,but it appeared " Matrix dimensions must agree.".Would you tell me more?

My code works if input_list is a row vector, as was your example, and as used in my example. if it were a column vector, you would need to apply a transpose to it. Debugging incompatible dimension is more of a MATLAB thing than a CVX thing, even for CVX code. The `whos` command can be very useful

If that is not enough to resolve your issue, you need to show your code, complete with all input data.

In any event, you should try to understand what my code does, and why it implements your constraint.

Sorry,‘a’ is a 21 matrix,and input_list is a 24 matrix.

I interpreted `a` as a scalar, and wrote my code accordingly. You should have made the necessary adjustments if you wanted `a` to be a vector.

I don’t even know what you mean by ‘a’ is a 21 matrix. is it 21 by 1? Or something else? Do you want every element of `a` to have to take a value from the list, but not necessarily the same value for the different elements? And same value for multiple elements allowed, etc.?

At this point, you should be able to figure out how to take the mathematical construct in my prreious post, and adapt it for your situation. Of course, you may need `b` to be suitably dimensioned, and will have to modify the `sum(b) == 1` constraint to apply separately to those elements of `b` corresponding to each element of `a`, or whatever the case is.

I will let you try to figure out what the necessary modifications are, so that you will learn something and become more self-sufficient.

‘a’ is a 2-by-1 matrix,input_list is a 2-by-4 matrix.I want ‘a’ is equivalent to one of the input_list.

I don’t understand what you want. But I believe you ought to be able to accomplish whatever it is exactly that you do want by using or suitably modifying the mathematical construct I showed.

Thank you very much!

I think what you want is

``````input_list =  [2 12 45 90;23 7 12 3];
variable b(size(input_list)) binary
a = sum(b.*input_list,2);
sum(b,2) == 1
``````

Each row of `a` consists of one element from the corresponding row of `input_list`.

This is what I want! Thank you so much! You have helped me a lot.

Dear Mark: I want to ask you about how to use the constraint expression in CVX correctly, such as variable vector a, matrix B ,A=aa^H,how to write (a^HBa) except for trace(BA), a^H represents the Conjugate transpose of a, (a^HBa) is not allowed in CVX, and how to express log2(variable), and How to write this as the CVX form? subject to D>log2(x*y)，where x and y are the new variables, thank you very much!

`log2(x)` can be written as `log(x)/log(2)`.

But `D > log(x*y)` is a non-convex constraint, and can’t be used in CVX.

I’m not clear what you’re asking regarding a, B,.
Presuming `B` is input data and is symmetric psd.
`trace(B*A) = trace(B*a*a') = trace(sqrtm(B)*a*a'*sqrtm(B)) = square_pos(norm(a'*sqrtm(B),'fro'))`
so the latter expression can be entered in CVX.

If `B` is input data and is symmetric psd, then `a'*B*a` is allowed in CVX. If `B` and `a` are both CVX variables, it is not allowed in CVX.

In the future, please start a new topic rather than posting a new question in an unrelated thread.

Thank you very much! I will try again

Hello Mark! I have met a new problem.When I introduce binary variables to choose a coordinate,the optional value is worse than the fixed coordinate.I really the reason!

Can you post a complete reproducible problem, and a clear explanation as to what it is doing differently than you think it should?

One of the constraints is to choose one of the four known coordinates to minimize the objective function, and I use binary variables to do that. But when I choose one of the coordinates myself, without using binary variables to solve, I find that the target function is smaller, and the coordinates I choose are not the same as those CVX chooses.

Apparently, you have not done something correctly, but unless you provide the requested information, readers of the forum will not be able to provide more specific guidance.

I use Matlab R2016a and R2018a respectively to run the same code, optimization value is not the same. I’m confused.

Does CVX claim to have solved both problem to optimality? is the optimal objective value the same (within tolerance)?

Please show us the code, preferably with all input data, and the solver and CVX output which shows what you are saying.

For examlpe:
chosed_list = [a1 b1 c1 d1;a2 b2 c2 d2];
variable A(size(chosed_list)) binary
Constrain:B == sum(A.*chosed_list,2);
sum(A,2) == 1;
I suppose that the optional value is (b1;b2);but when I use “B == (a1;a2)”,the objective value is better.

You haven’t shown us a complete program, including an objective function. If there is no objective function, any feasible solution is as good as any other.