Multiply multi-dimensional array with vector

Uncategorized
Apr 11, 2022
A

Hi,
I am trying do something like this:

cvx_begin
variable theta(n,n,m) nonnegative
expression tMode3Prod;
lamdaR = reshape(lamda,1,1,[]);
tMode3Prod = squeeze(sum((theta.lamdaR),3));
expression obj1;
obj1 = sum(sum(aMat .
tMode3Prod));

expression tMode2Prod;
tMode2Prod  = squeeze(sum(theta,2));
expression term2Mat;
term2Mat = b - tMode2Prod;
expression obj2;
obj2 = C*(square_pos(norm(term2Mat, 'fro')));

minimize(obj1+obj2)
subject to 
    sum(sum(theta)) == 1;

cvx_end

When I run this CVX gives an error: “Error using .* Matrix dimensions must agree.” . This error is for the operation "tMode3Prod = squeeze(sum((theta.*lamdaR),3)); " , please note that lamda is of dim (mX1) and hence lamdaR is of (1X1Xm) . If i do the same operation in matlab with a tensor of size (n,n,m), it doesn’t give any error. The error is bcoz theta is of datatype “cvx” and “lamdaR” is double. What should I do ?

Thank you

J

try let lamdaR= permute(repmat(lamda,1,n,n),[2 3 1]) to do what you want. I would say cvx is very unlikely to cause a dimension issue like that, even it does, from your description, it is clear your dimension doesn’t agree, one is 1x1xm,another is nxnxm.

A
Replying to #2

But want to perform multiplication across only last dimension. Thats why I am doing a re-shape operation: lamdaR = reshape(lamda,1,1,[]), and then using this.
I have verified, this works well and correctly but in cvx its giving error

M

is what you are doing in MATLAB (i.e., double precision variables) making use of implicit expansion https://blogs.mathworks.com/loren/2016/10/24/matlab-arithmetic-expands-in-r2016b/ ?

CVX does not support implicit expansion, and likely never will.

A
Replying to #5

Thanks a lot for your reply, then how should I go about it in CVX ? Any suggestions ?

J
Replying to #6

do as i told you, it will work. just think about it, even you just want to do to the last dimension, the result will definetly be a nXnXm array. how can you get an array like that without expandsion? or, please use mathematical formula or a loop to illustrate what you actually mean.

you can actually test in matlab whether what i told you is correct.

A
Replying to #7

https://in.mathworks.com/matlabcentral/answers/609866-multiply-an-array-of-scalars-by-a-3d-matrix , this is what I want to achieve .

J
Replying to #8

yes, that is what i did.

please test it in matlab. it is easy to do. just use a loop to do what you want, then use that formular i give you, see whether they produce the same thing.

M

Just for the record, because bsxfun is mentioned as an alternative to implicit expansion in the link a couple of posts back, bsxfun is not supported by CVX.

The example, which predates MATLAB introduction of implicit expansion, or the MATLAB term “implicit expansion”, is kind of a pre(non)implicit expansion way of doing implicit expansion.

A
Replying to #10

So which approach you suggest Mark ? Thank you

M

Follow @jackfsuia 's advice.

A
Replying to #12

@Mark_L_Stone @jackfsuia thanks it worked