I have a CVX code and I want to convert it into Matlab code

M = 3;
N = 2;
r =[3.0000 + 0.0000i 0.5595 - 0.0767i -0.5694 - 0.7981i 0.5595 - 0.0767i -0.5694 - 0.7981i 1.9114 + 0.9896i]’;
w = sqrt(noiseVar / 2) * (randn(size®) + 1j * randn(size®));
y = r+w;
noiseVar=0.030612392311413;
lambda = 2sqrt(MNlog(MN))sqrt(noiseVar);
cvx_begin sdp quiet
variable x(N
M,1) complex
variable F(MN,MN) hermitian
minimize(norm(y-x))
subject to
[F, x; x’, lambda^2]>=0
for idxN1 = 0 : 1 : N-1
for idxN2 = 0 : 1 : N-1
Ftmp = F(idxN1M+1:idxN1M+M, idxN2M+1:idxN2M+M);
for delta = -(M-1) : 1 : M-1
if ((idxN1==idxN2) && (delta==0))
sum(diag(Ftmp, delta)) <= 1/N;
else
sum(diag(Ftmp, delta)) == 0;
end
end
end
end
cvx_end

Please be more clear. This is already Matlab code. And if you want to convert it from CVX into something else than CVX then it is possibly a question outside of the scope of this forum.

Thanks a lot for your kind response. Yes you are right. The beginning 7 lines are in Matlab but after that the code is in CVX which i re-produce below. I don’t understand it.So if this is converted into Matlab code, i will be able to understand it.

cvx_begin sdp quiet
variable x(NM,1) complex
variable F(MN,MN) hermitian
minimize(norm(y-x))
subject to
[F, x; x’, lambda^2]>=0
for idxN1 = 0 : 1 : N-1
for idxN2 = 0 : 1 : N-1
Ftmp = F(idxN1M+1:idxN1M+M, idxN2M+1:idxN2M+M);
for delta = -(M-1) : 1 : M-1
if ((idxN1==idxN2) && (delta==0))
sum(diag(Ftmp, delta)) <= 1/N;
else
sum(diag(Ftmp, delta)) == 0;
end
end
end
end
cvx_end

If you want to understand an existing CVX program, start off by reading the CVX Users’ Guide. Of course, that presumes some knowledge of mathematical optimization in order to understand. That particular program is an SDP, so unless you know something about SDPs, you won’t have any idea what it is about. To learn about SDPs (and more generally, convex optimization), read https://web.stanford.edu/~boyd/cvxbook/ .

Before even attempting to run it, you should remove quirt, so that you will see CVX and solver output.

Thanks a lot for your kind response. Actually I just want its Matlab equivalent code as I am going to use it in my work. So if you kindly can make its Matlab equivalent, it will help me. Regards,

This is MATLAB code, making use of the CVX toolbox. There is no MATLAB “equivalent” without calling an SDP solver.

If you install CVX, which includes some free SDP solvers, you should be able to run the code as is.

Thanks a lot for your kind response. Yes, I have installed CVX in my Matlab and it runs too but I don’t understand it. So I wanted if someone converts it into Matlab equivalent code, then I will be able to understand it well. And then even I can do changes in it also.

There is no MATLAB equivalent code. Follow my advice in previous posts if you want to understand the code. If your mathematical background is not strong enough, that might not be easy.

Formulation and solution of SDP optimization problems is not part of, or expressible in base MATLAB, and is only addressed with toolboxes, of which CVX is one, and which have their own syntaxes.

Thanks a lot for your kind response. Can you explain what is the meaning of these expressions:
sdp quiet
variable x(NM,1) complex
variable F(M
N,M*N) hermitian

Are the 2nd and 3rd expressions the declaration of variables just like in C/C++?

The first line invokes sdp mode and suppresses CVX and solver output. The second and 3rd lines declare optimization (decision) variables, i.e., the variables whose optimal values are to be found.

Given your questions, it is obvious you have not read the CVX Users’ Guide which I said you should do in my first post. Please do so before asking further questions…

Thanks a lot for your response. Yes you are right. I didn’t read the CVX Users’ Guide. But now I studied it and it gave me a book which I started but I don’t see any help of CVX language as you told me in the above post the meaning of the three lines. My study speed is very slow. I will try to read it with the lapse of time.But fot the time being I am stuck in my work and I have to show to my co-mentor what did I do with the code?
So please tell me what is the meaning of the following lines:

minimize(norm(y-x))
subject to
[F, x; x’, lambda^2]>=0

CVX is designed in such a way that most of the time you should be able to read it as if you were reading a paper and everything would be written using mathematical notation. For example the above snippet simply means

\mathrm{minimize} \|y-x\|_2

\mathrm{subject}\ \mathrm{to} \left[\begin{array}{cc}F & x\\x' & \lambda^2\end{array}\right]\succeq 0

Good luck with your study.

Thanks a lot for your kind responses. Your explanation helps me a lot now. What is the meaning of these lines:

Ftmp = F(idxN1M+1:idxN1M+M, idxN2M+1:idxN2M+M);
sum(diag(Ftmp, delta)) <= 1/N;

Ftmp is indexing into the CVX matrix variable F and the sum statement is summing over certain elements of that and constraining it to be <= 1/N…

f you instead made F a numerically populated MATLAB double precision variable, Ftmp and the sum statement would do some processing on it. When F is a CVX variable, those same operations are done, but `F is unknown and the solver finds the optimal value.

If you want to understand the program, you need the right mathematical background, which seems rather in question, and to have understood the CVX Users’ Guide. Perhaps you should be directing further questions to the program’s author. On the other hand, if you are a student, I would think you are expected to put in significant effort on your own, rather than relying on other people to figure everything out for you.

1 Like

Thanks a lot for your kind response. Yes, you are right.