# Set some values to zero

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

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``````

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

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.

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]

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`

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

I wrote it in the subject to part

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

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

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}

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.

1 Like

No thatâ€™s great, thank you!

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

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

1 Like

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.

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.

1 Like