# Formulating minimization problem in matlab with three dimensional matrix?

``````    mf = 383;
numclus = 5;
clear cvx;
cvx_begin quiet
variables U(mf,mf,numclus) lambda(1)
expressions S(1)
S = 0;
for i = 1:mf-1
for j = 1:mf-1
for k = 1:numclus
S = S + U(i,j,k)*E(i,j,k) + lambda*((U(i,j+1,k)-U(i,j,k))^2 + (U(i+1,j,k)-U(i,j,k))^2);
end
end
end
minimize(S)
subject to
sum(U,3) == 1
U >= 0
lambda >= 0
cvx_end
``````

Above is my attempt at a minimization problem. The first term in the sum is a distance minimizer with E being a matrix of values that I calculated prior to running cvx. The later term is like a “gradient” for U. The error that Matlab is spitting out is:

``````Disciplined convex programming error:
Cannot perform the operation: {real affine} .* {convex}
``````

However, all the values I am multiplying are not vectors? I am not sure whats going on here. I know the code is not very efficient, I am still trying to figure out how to write everything in matrix notation with the use of for loops but this is the problem I have now. Please any feedback is appreciated! Am very desperate!

As a side note, these matrices are pretty big and I was wondering if there was a way for cvx to stop after a certain amount of time or to have it spit out how long its taking (using tic and toc?)

I believe the issue here is that `lambda*((U(i,j+1,k)-U(i,j,k))^2` is not jointly convex in `lambda` and `U`. CVX is only for solving convex problems. See the CVX user guide for a list of acceptable expressions.

ah, good to know. I just removed that part from my response.