"permute" command for sdpvar

24 views
Skip to first unread message

reza mehrabadi

unread,
Dec 22, 2018, 1:46:42 AM12/22/18
to YALMIP
Hi All. Due to time problems with "for" loops, i wanted to restate the following code (located inside 4 "for" loops):
pij=sdpvar(B,B,T,H);
.
.
.
constr
=[constr,pij(i,j,t,h)+pij(j,i,t,h)==0];


changed to (without need for any loop):

constr=[constr,pij+permute(pij,[2 1 3 4])==0];

Error in sdpvar/permute (line 8)
X.dim = X.dim(p);


Johan Löfberg

unread,
Dec 22, 2018, 3:35:01 AM12/22/18
to YALMIP
What dimensions do you have. Works here, and the error message signals that you don't have an nd-array (called ndsdpvar internally) but a standard 2d array (sdpvar) due to singleton trailing dimensions

>> X = sdpvar(2,3,4,5);
>> permute(X,[4 3 1 2])
Multi-dimensional SDPVAR object 5x4x2x3
>> X = sdpvar(2,3,1,1);
>> permute(X,[4 3 1 2])
Index exceeds array bounds.

reza mehrabadi

unread,
Dec 22, 2018, 3:47:09 AM12/22/18
to YALMIP
Well. Thanks. Yes the size has single dimensions like: 
pij=sdpvar(39,39,1,1,'full');
permute(X,[2 1 3 4]);

I can change the size but anyway to work with singles?

Johan Löfberg

unread,
Dec 22, 2018, 4:26:36 AM12/22/18
to YALMIP
it's simply not supported at the moment, you will have to do some clever combination of standard 2d matrix + permute  + reshape or something

>> x = sdpvar(2);reshape(permute(x,[2 1]),1,1,2,2)
Multi-dimensional SDPVAR object 1x1x2x2

or somehing like that, perhaps

Johan Löfberg

unread,
Dec 22, 2018, 10:13:36 AM12/22/18
to YALMIP
add to top of sdpvar/permute
if length(X.dim) < length(p)
    X = ndsdpvar(X);        
    X = permute(X,p);
    return
end

and this to top of ndsdpvar/permute
if length(X.dim) < length(p)
    X
.dim = [X.dim ones(1,length(p)-length(X.dim))];
end

after that, it should work
>> X = sdpvar(2,3);
>> Y = randn(2,3);assign(X,Y);
>> d = permute(X,[4 3 2 1])-permute(Y,[4 3 2 1]);norm(value(d(:)))

ans =

     0



reza mehrabadi

unread,
Dec 22, 2018, 11:33:24 AM12/22/18
to YALMIP
Thank you very much. I've changed single dimensions into something else and problem solved (in addition to significant yalmiptime reduction). Maybe you can apply this to the next version!

Johan Löfberg

unread,
Dec 22, 2018, 11:45:41 AM12/22/18
to YALMIP
Yes this is of course added to the code as it is expected MATLAB syntax
Reply all
Reply to author
Forward
0 new messages