 # Cvx error: Conversion to double from cvx is not possible

I’m trying to minimize a trace function subject to a norm constraint. Here’s my code in Matlab:

``````function q=min_prob(y,t)
sz1=size(y,1);
sz2=size(y,2);

for i=1:10
cvx_begin sdp quiet
cvx_precision low
variable z(sz1,sz2)

minimize(trace(y'*z))
subject to
cvx_end
end
end

x=reshape(x,sz1,sz2);
% d1=[x(1,:);diff(x,1,1)];
% d2=[x(:,1)';diff(x,1,2)']';
d1=zeros(sz1,sz2);
d2=zeros(sz1,sz2);
d1(1,:)=x(1,:);
d2(:,1)=x(:,1);
for i=1:sz1-1
d1(i+1,:)=x(i+1,:)-x(i,:);
end
for i=1:sz2-1
d2(:,i+1)=x(:,i+1)-x(:,i);
end

y=d1+d2;
end
``````

cvx doesn’t recognize the inbuilt ‘diff’ function, so I wrote out the function explicitly.
But when I run the code I get the following error in the grad2 function at `d1(1,:)=x1(1,:)`: “Conversion to double from cvx is not possible”.
I’ve tried using `expression z(sz1,sz2)`, it doesn’t work.

I’m surprised I never implemented `diff`. It might be because of the various options therein. I will try to rectify that for CVX 3.0.

To fix your specific problem, try changing

``````d1=zeros(sz1,sz2);
d2=zeros(sz1,sz2);
``````

to

``````d1=cvx(zeros(sz1,sz2));
d2=cvx(zeros(sz1,sz2));
``````

As the error indicates, the problem is that you’re trying to assign a CVX variable to a slice of a standard numeric array. Matlab can’t be taught to catch that and “promote” the double array to a CVX variable automatically, so you have to do it yourself.

Sir,

I tried your solution out but it doesn’t seem to be working. I also have an optimization problem wherein there is updation in a variable and exactly the same error is happening. I am working with cvx on MATLAB 18.