Set some values to zero


(Menna Khaled) #1

if my variable is a 3D array and I just want to set some values in this array to zero, is this doable ?


(Mark L. Stone) #2

As long as the indices are numerical values or MATLAB variables, but not CVX variables, then yes. For example,

I = [1 3];
J = 2;
variable X(10,5,6)
X(I,J,:) == 0

(Menna Khaled) #3

No unfortunately I need to do sth similar to this in 1D:

cvx_begin gp
variable P(n)

and then set for example th 2nd and 5th position in P to zeros


(Mark L. Stone) #4

Please provide a small example of a 3D variable, and the specific elements of it which you would like to set to zero, but don’t know how.


(Menna Khaled) #5

for example :
cvx_begin gp
variable P(nU,nG,nR)
.
.
cvx end

and I’d like to set the locations in p corresponding to zeros in f to zero:
f(:,:,1) = [ 0 1;1 0;0 0];
f(:,:,2) = [ 1 0;0 0;1 0];
f(:,:,3) = [ 0 0;0 1;0 1];

I mean the output from cvx should be sth like this:
[0 2;3 0;0 0]
[.2 0;0 0 ;1.1 0]
[0 0 ; 0 9.2; 0 1]


(Mark L. Stone) #6

I will presume f is an input (MATLAB) variable, not a CVX variable. If f is a CVX variable, then what you are trying to do is non-convex and I think you’d have to use the MIDCP capability to get what you want.

Is this what you want?
P(f == 0) == 0


(Menna Khaled) #7

P(f ==0)=0 yes
when I wrote this before cvx end he gave me an error


(Menna Khaled) #8

I wrote it in the subject to part


(Mark L. Stone) #9

You need == on the right-hand side, which is a CVX constraint, not =, which is an assignment.

Equivalently, you could do
P(-f) == 0


(Menna Khaled) #10

Thank you, you’re very helpful !

However, when I did what you said, I got this error
Disciplined convex programming error:
Invalid constraint: {log-affine} == {constant}


(Mark L. Stone) #11

Sorry, I wasn’t paying attention to your doing gp. Without gp, my code would work.

If you are doing gp, I can’t think of a more elegant way than building up an expression which inserts the zeros where you want them. For instance, if you want a 6 by 1 vector with zeros in the 2nd and 5th components,

variables x y(2) z
P = [x;0;y;0;z]

I’ll leave you to work out the details in your case.

Perhaps someone else can provide a more elegant solution.


(Menna Khaled) #12

No that’s great, thank you!

It was really helpful !

Is there something by which I can check the status ?
for example:
if (problem.status.equals(infeasible))
do sth
else
do sth ??


(Mark L. Stone) #13

cvx_status http://cvxr.com/cvx/doc/solver.html#interpreting-the-results


(Menna Khaled) #14

perfect thanks!

I was wondering whether it makes any difference to put the calculations part before or after the “subject to” or “minimize” part.

I mean what I get is that before using a certain variable, it needs to be defined, which means that its calculations need to done. So, before or after the “subject to” or “minimize” part doesn’t matter.


(Mark L. Stone) #15

As long as variables and expressions which are used are declared and defined prior to use, the objective function (minimize or maximize) and constraints can appear in any order. “subject to” is optional and doesn’t actually do anything - it’s available for use to help you make your code clearer - I never bother with it myself.