1 | function y = stackcell(dummy,blocks) |
---|
2 | %STACKCELL Internal function for rapid concatenation |
---|
3 | % |
---|
4 | % [x1 x2 ... xn] is written as stackcell(sdpvar(1,1),{x1,x2,x3,...,xn}) |
---|
5 | % Why the first argument? A hack to make it a method for sdpvar... |
---|
6 | |
---|
7 | |
---|
8 | % Author Johan Löfberg |
---|
9 | % $Id: stackcell.m,v 1.5 2006/07/26 20:17:58 joloef Exp $ |
---|
10 | |
---|
11 | nblocks = size(blocks,2); |
---|
12 | % Get dimensions |
---|
13 | n = zeros(nblocks,1); |
---|
14 | m = zeros(nblocks,1); |
---|
15 | isasdpvar = zeros(nblocks,1); |
---|
16 | % Get dimensions |
---|
17 | for i = 1:nblocks |
---|
18 | [n(i),m(i)]=size(blocks{i}); |
---|
19 | isasdpvar(i) = isa(blocks{i},'sdpvar'); |
---|
20 | end |
---|
21 | |
---|
22 | if ~any(isasdpvar) |
---|
23 | y = blocks{1}; |
---|
24 | for i = 2:length(blocks) |
---|
25 | y = [y blocks{i}]; |
---|
26 | end |
---|
27 | return; |
---|
28 | end |
---|
29 | |
---|
30 | % Keep only non-empty |
---|
31 | keep_these = find((n.*m)~=0); |
---|
32 | if length(keep_these)<length(n) |
---|
33 | blocks = {blocks{keep_these}}; |
---|
34 | n = n(keep_these); |
---|
35 | m = m(keep_these); |
---|
36 | isasdpvar=isasdpvar(keep_these); |
---|
37 | nblocks = length(n); |
---|
38 | end; |
---|
39 | |
---|
40 | % Find all free variables used |
---|
41 | all_lmi_variables = []; |
---|
42 | for i = 1:nblocks |
---|
43 | if isasdpvar(i) |
---|
44 | all_lmi_variables = [all_lmi_variables blocks{i}.lmi_variables]; |
---|
45 | end |
---|
46 | end |
---|
47 | all_lmi_variables = uniquestripped(all_lmi_variables); |
---|
48 | |
---|
49 | % Pick one of the sdpvar objects to build on... |
---|
50 | y = blocks{min(find(isasdpvar))}; |
---|
51 | |
---|
52 | % Some indexation tricks |
---|
53 | n = n(1); |
---|
54 | allindextopos = (sparse(1,[1;1+all_lmi_variables(:)],1:1+length(all_lmi_variables),1,1+all_lmi_variables(end))); |
---|
55 | |
---|
56 | basis = []; |
---|
57 | basis_i = []; |
---|
58 | basis_j = []; |
---|
59 | basis_s = []; |
---|
60 | shft = 0; |
---|
61 | for j = 1:nblocks |
---|
62 | if isasdpvar(j) |
---|
63 | in_this = find(ismembc(all_lmi_variables,blocks{j}.lmi_variables)); |
---|
64 | dummy = [1 1+in_this]; |
---|
65 | [i2,j2,s2] = find(blocks{j}.basis); |
---|
66 | j2 = dummy(j2); |
---|
67 | add_shift = size(blocks{j}.basis,1); |
---|
68 | else |
---|
69 | [i2,j2,s2] = find(blocks{j}(:)); |
---|
70 | add_shift = size(blocks{j}(:),1); |
---|
71 | end |
---|
72 | basis_i = [basis_i;i2(:)+shft]; |
---|
73 | basis_j = [basis_j;j2(:)]; |
---|
74 | basis_s = [basis_s;s2(:)]; |
---|
75 | shft = shft + add_shift; |
---|
76 | end |
---|
77 | basis = sparse(basis_i,basis_j,basis_s,sum(m)*n,1+length(all_lmi_variables)); |
---|
78 | |
---|
79 | y.dim(1) = n; |
---|
80 | y.dim(2) = sum(m); |
---|
81 | y.basis = basis; |
---|
82 | y.lmi_variables = all_lmi_variables; |
---|
83 | |
---|