Assigning zero values to some elements of sdpvar takes too long

88 views
Skip to first unread message

Ihsan Yanikoglu

unread,
Nov 22, 2013, 1:12:21 PM11/22/13
to yal...@googlegroups.com
Dear Johan,

The below operation takes too long considering that there are only 2 for loops (2000 operations take around 5-7min):

n=10;
m
=20;

Y = sdpvar(n*m,m+1);

for j=2:1:m+1
    for i=1:1:n*m
       if i<1+(j-2)*m || i>(j-1)*m
           Y(i,j)=0;
       end
    end
end

Is there a more efficient way of doing that operation in YALMIP, since I would do similar operations many times (basically I want make some variables = some constant in sdpvar's).

Thanks in advance, Ihsan
Message has been deleted
Message has been deleted

Ihsan Yanikoglu

unread,
Nov 22, 2013, 5:40:52 PM11/22/13
to yal...@googlegroups.com
ctually the following created the same matrix in a sec

n=10;
m
=20;
= [sdpvar(n*m,1)];
for i=1:1:n
 Y
(1+(i-1)*m:i*m,i+1)=sdpvar(m,1);
end

So, if we create Y as sdpvar(n*m, m+1) first, and assign zero elements later the same matrix is created in 7min. Seems like if we do it in reverse order it is so inefficient.

I update my question: Why is the performance of the two codes are so different ?

Johan Löfberg

unread,
Nov 25, 2013, 6:38:27 AM11/25/13
to yal...@googlegroups.com
The first operation is completely elementwise, and uses subasgn which has some performance issues. The second approach is vectorized, which almost always leads to more efficient code.

The first approach would more efficiently be encoded as (separate simple data generation from symbolic manipulations)

n=10;
m
=20;
Y
= sdpvar(n*m,m+1);

bad
= [];

for j=2:1:m+1
   
for i=1:1:n*m
       
if i<1+(j-2)*m || i>(j-1)*
m
           bad
= [bad;i j];
       
end
   
end
end
index
= sub2ind([n*m,m+1],bad(:,1),bad(:,2));
Y
(index)=0;

However, I belive you can create your object most efficiently by some clever kron operator or something like that
Reply all
Reply to author
Forward
0 new messages