Multiplication operation

Uncategorized
Dec 22, 2012
K

Good day, I have a problem with a multiplication operation in CVX.

cvx_begin
variables alfa(m) temp;
maximize sum(alfa) - 0.5*temp;
subject to
    temp = 0;
    for i=1:m
        for j=1:m
            temp = temp + alfa(i)*alfa(j)*labels(i)*labels(j)*K_matrix(i,j);
        end
    end
    0 <= alfa <= C;
    sum(alfa.*labels) == 0;
cvx_end

And output:

Disciplined convex programming error:
Invalid quadratic form(s): not a square.
....
Error in main (line 23)
temp = temp + alfa(i)*alfa(j)*labels(i)*labels(j)*K_matrix(i,j);

So for sure I do the operation in a wrong way, but can you give me advice how to change it to a proper form?

M

I would eliminate temp altogether—in fact, the temp = 0 term will eventually force an error when you reach cvx_end, for the reasons explained in Assignment and expression holders.

Then I would replace the objective term with this:

minimize (alfa.*labels)'*K_matrix*(alfa.*labels)

I think you should be able to confirm by inspection that this is correct.

K

I’ve changed my code to this for using your advice

temp = (alfa.*labels)';
maximize sum(alfa) - 0.5*temp*K_matrix*(alfa.*labels);

And it seems to work properly now. So thanks.

M

Very good, but why use temp here at all? And if you insist on using temp, why not use it for both the left and right-hand sides of the quadratic form? It seems you’re simply making your code less readable for no practical reason.