Problem with using the max while manipulating a 3-dimensional array

633 views
Skip to first unread message

Krzysztof Postek

unread,
Sep 9, 2014, 10:56:24 AM9/9/14
to yal...@googlegroups.com
Dear Mr. Lofberg,

I have a problem that while defining the optimization problem involving 3 dimensional matrices (attached with data definitions) I get the following message: 

Error using ndsdpvar.max
Too many input arguments.

Could you please help me with this issue?

Thank you
Krzysztof
Inventory.m
Message has been deleted

Johan Löfberg

unread,
Sep 9, 2014, 2:30:23 PM9/9/14
to yal...@googlegroups.com
Hmm, I should have run your code first...

Johan Löfberg

unread,
Sep 9, 2014, 2:33:14 PM9/9/14
to yal...@googlegroups.com
An easy way out for me is to say that your code is not valid MATLAB syntax

X = randn(2,3,4);
Y
= randn(2,3,4);
>> max(X,Y,3)
Error using max
MAX
with two matrices to compare and a working dimension is not supported.


Johan Löfberg

unread,
Sep 9, 2014, 2:45:02 PM9/9/14
to yal...@googlegroups.com
I don't even understand what it means to take max(X,Y,dim). What would be the answer here for dim=2, i.e, the highest dimension

>> X = [1 2;3 4]

X
=

     
1     2
     
3     4

>> Y = [5 6;7 8]

Y
=

     
5     6
     
7     8


Krzysztof Postek

unread,
Sep 10, 2014, 6:52:29 AM9/10/14
to yal...@googlegroups.com
Dear Mr. Löfberg

thank you for your reply. The following code is rather ok with respect to parameters, but gives me exactly the same error:

q=sdpvar(2,1);


objective_proxy
= sdpvar;


Constraints = [norm(q,1) <= 1];
Constraints = [Constraints; max(rand(2,2,2).*repmat(q,[1 2 2]), rand(2,2,2).*repmat(q,[1 2 2])) <= objective_proxy];


Objective = objective_proxy;


sol
= solvesdp(Objective,Constraints);

I think the reason is that the max.m function from the ndsdpvar folder interprets the 3rd dimension of the matrices as an extra parameter of max, and hence the error. However, I just tackled the problem for my application by interchanging the dimensions of the matrices, so that I no longer compare 3D matrices with max, but only 2D matrices.

Johan Löfberg

unread,
Sep 10, 2014, 7:01:00 AM9/10/14
to
This case is correct MATLAB syntax, but the case with two arguments is not implemented for ndsdpvars

You can write it as
a = rand(2,2,2).*repmat(q,[1 2 2]);
b
= rand(2,2,2).*repmat(q,[1 2 2]);
reshape
(max(a(:),b(:)),size(a))

The next version will support this though

Johan Löfberg

unread,
Sep 10, 2014, 7:04:36 AM9/10/14
to yal...@googlegroups.com
Ah well, here it is

function Y = max(X,aux,workdim)
% abs (overloaded)

switch nargin
   
case 1
        workdim
= 1;
   
case 2
        Y
= max(reshape(X,[],1),reshape(aux,[],1));
        Y
= reshape(Y,size(X));
       
return
   
case 3
       
if ~isempty(aux)
            error
('MAX with two matrices to compare and a working dimension is not supported in MATLAB.');
       
end
    otherwise
        error
('Too many input arguments')
end

xdim
= size(X);

if workdim > length(xdim)
    error
('Index exceeds matrix dimensions.');
end

newdim
= xdim;
newdim
= circshift(xdim',-(workdim-1))';
newdim
(1)=1;
Y
= reshape(max(reshape(shiftdim(X,workdim-1),xdim(workdim),[])),newdim);
Y
= shiftdim(Y,length(xdim)-workdim+1);


Krzysztof Postek

unread,
Sep 10, 2014, 6:48:52 PM9/10/14
to yal...@googlegroups.com
Great! Thank you, I will use the upgraded version in my next numerical example! Sometimes it is just easier to work in mind with entire 3D variables.
Reply all
Reply to author
Forward
0 new messages