# Matrix dimension must agree error

Hi there, I am running cvx but I run into this error.

Error using + (line 45)
Matrix dimensions must agree.
Error in multi_power (line 14)
(1/N)sum(sum(a(k,:,:).(log(sum(P_crl.h(k,:,:), 3) + sigma_2)/log(2) - (D(k,:,:).(P_crl - P) + log(E(k,:,:))/log(2))), 3), 2) >= eta_pow;

My code is as follows:

function [result,output] = multi_power(P, K, N, M, P_max, a, h, sigma_2, D, E)
cvx_begin quiet
variables P_crl(1,N,M)
variable eta_pow
maximize eta_pow
subject to
for m = 1:M
for n = 1:N
0 <= P_crl(1,n,m) <= P_max;
end
end

``````            for k = 1:K
(1/N)*sum(sum(a(k,:,:).*(log(sum(P_crl.*h(k,:,:), 3) + sigma_2)/log(2) - (D(k,:,:).*(P_crl - P) + log(E(k,:,:))/log(2))), 3), 2) >= eta_pow;
end
cvx_end
result = cvx_optval;
output = P_crl;
``````

end

In this part, variables a, h, D, E are of dimension (K,N,M), P is of dimension (1,N,M). And sigma_2 and P_max are just a number. I have no idea why it keeps telling me dimension error. Any help is greatly appreciated! Thanks!

Another thing that confuses me is that when I replace the variable P_crl with a known P, which should be of the same size, the equation will show no error. It means the LHS of Line 14 can have a certain value if I use P instead. But P_crl and P should be of the same dimension so I really have no idea why it doesn’t work when I try to use P_crl.

Try using squeeze on the 1 by something by something variables. Or reshape to 2D. MATLAB won’t let you get away with not doing this for matrix multiply, but does let you get away with not doing it for .`*` . CVX might be fussier than MATLAB, and certainly not less fussy about those things.

Hello Mark. I tried using squeeze to make everything inside a 2D matrix and reduce the dimension in summation, like this:

``````(1/N)*sum(sum(squeeze(a(k,:,:)).*(log(sum(squeeze(P_crl).*squeeze(h(k,:,:)), 2) + sigma_2)/log(2) - (squeeze(D(k,:,:)).*(squeeze(P_crl) - squeeze(P)) + log(squeeze(E(k,:,:)))/log(2))), 2), 1) >= eta_pow;
``````

Sorry this is a long inequality. but it shows a matrix dimension disagree error. So I tried removing the term P_crl and I have:

``````(1/N)*sum(sum(squeeze(a(k,:,:)).*(log(sum(squeeze(P).*squeeze(h(k,:,:)), 2) + sigma_2)/log(2) - (squeeze(D(k,:,:)).*(- squeeze(P)) + log(squeeze(E(k,:,:)))/log(2))), 2), 1) = 1.2650, which is computable.
``````

Why does this happen? What does this mean?

Perhaps you should start from simple subexpressions (X op Y), and gradually build up as necessary, and find out where specifically the error is occurring. If things work when all CVX variables are replaced by same-dimensioned MATLAB double precision variables, then I think it should work when some of them are CVX variables, provided that implicit expansion (which is not supported by CVX) s not used. https://blogs.mathworks.com/loren/2016/10/24/matlab-arithmetic-expands-in-r2016b/ .

You might make things simpler for yourself if you make the k index the 3rd dimension, rather than the first. Then squeeze or reshape will not be needed because matrix(:,:,k) will be a 2D matrix, not a 3D object with a singleton dimension.If there is some CVX limitation or bug, perhaps you would then avoid it, and the code will look cleaner without squeeze or reshape.

Hello Mark,

If things work when all CVX variables are replaced by same-dimensioned MATLAB double precision variables, then I think it should work when some of them are CVX variables

That is exactly what I was trying before, by replacing cvx variable P_crl with predefined same dimensional constant P. But the error still occurs. I am completely confused…

You posted just before the edit of my post was complete in which I added another paragraph which might help you.

Hello Mark, I changed all 3-dimensional variables including cvx variables to size(N,M,K), like this

``````(1/N)*sum(sum(a_pow(:,:,k).*(log(sum(P_crl.*h_pow(:,:,k), 2) + sigma_2)/log(2) - (D(:,:,k).*(P_crl - P_pow) + log(E(:,:,k))/log(2))), 2), 1) >= eta_pow;
``````

But the dimension error still exists. Do you have any other ideas? Thank you so much!

As I wrote before:
Perhaps you should start from simple subexpressions (X op Y), and gradually build up as necessary, and find out where specifically the error is occurring.

In general simplify, until you find the simplest example illustrating the problem. Then create a reproducible version of the example, complete with all input data. I suspect a very short program with very small amount of data would be sufficient. Also, make liberal use of the whos command, including applying it to CVX expressions (or just type the expression at the command line to see what CVX says).