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

# Set some values to zero

**Mark_L_Stone**(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**(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**(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**(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**(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`

**Mark_L_Stone**(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**(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**(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**(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**(Mark L. Stone) #13

**menna_khaled**(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**(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.