1 | function varargout = subsref(varargin) |
---|
2 | %SUBSREF (overloaded) |
---|
3 | |
---|
4 | % Author Johan Löfberg |
---|
5 | % $Id: subsref.m,v 1.1 2006/08/10 18:00:22 joloef Exp $ |
---|
6 | |
---|
7 | % Stupid first slice call (supported by MATLAB) |
---|
8 | if length(varargin{2}.subs) > 2 |
---|
9 | i = 3; |
---|
10 | ok = 1; |
---|
11 | while ok & (i <= length(varargin{2}.subs)) |
---|
12 | ok = ok & (isequal(varargin{2}.subs{i},1) | isequal(varargin{2}.subs{i},':')); |
---|
13 | i = i + 1; |
---|
14 | end |
---|
15 | if ok |
---|
16 | varargin{2}.subs = {varargin{2}.subs{1:2}}; |
---|
17 | else |
---|
18 | error('??? Index exceeds matrix dimensions.'); |
---|
19 | end |
---|
20 | |
---|
21 | end |
---|
22 | |
---|
23 | if (isa(varargin{2}.subs{1},'sdpvar')) | (length(varargin{2}.subs)==2 & isa(varargin{2}.subs{2},'sdpvar')) |
---|
24 | % ***************************************** |
---|
25 | % Experimental code for varaiable indicies |
---|
26 | % ***************************************** |
---|
27 | varargout{1} = milpsubsref(varargin{:}); |
---|
28 | return |
---|
29 | else |
---|
30 | X = varargin{1}; |
---|
31 | Y = varargin{2}; |
---|
32 | end |
---|
33 | |
---|
34 | try |
---|
35 | switch Y.type |
---|
36 | case '()' |
---|
37 | % Check for simple cases to speed things up (yes, ugly but we all want speed don't we!) |
---|
38 | switch size(Y.subs,2) |
---|
39 | case 1 |
---|
40 | if isa(Y.subs{1},'sdpvar') |
---|
41 | varargout{1} = yalmip('addextendedvariable',mfilename,varargin{:}); |
---|
42 | return |
---|
43 | else |
---|
44 | y = subsref1d(X,Y.subs{1}); |
---|
45 | end |
---|
46 | case 2 |
---|
47 | y = subsref2d(X,Y.subs{1},Y.subs{2}); |
---|
48 | otherwise |
---|
49 | error('Indexation error.'); |
---|
50 | end |
---|
51 | otherwise |
---|
52 | error(['Indexation with ''' Y.type ''' not supported']) ; |
---|
53 | end |
---|
54 | catch |
---|
55 | error(lasterr) |
---|
56 | end |
---|
57 | if isempty(y.lmi_variables) |
---|
58 | y = full(reshape(y.basis(:,1),y.dim(1),y.dim(2))); |
---|
59 | else |
---|
60 | % Reset info about conic terms |
---|
61 | y.conicinfo = [0 0]; |
---|
62 | end |
---|
63 | varargout{1} = y; |
---|
64 | |
---|
65 | function X = subsref1d(X,ind1) |
---|
66 | |
---|
67 | % Get old and new size |
---|
68 | n = X.dim(1); |
---|
69 | m = X.dim(2); |
---|
70 | |
---|
71 | % Convert to linear indecicies |
---|
72 | if islogical(ind1) |
---|
73 | ind1 = double(find(ind1)); |
---|
74 | end |
---|
75 | |
---|
76 | % Ugly hack handle detect X(:) |
---|
77 | %pickall = 0; |
---|
78 | if ischar(ind1) |
---|
79 | X.dim(1) = n*m; |
---|
80 | X.dim(2) = 1; |
---|
81 | return; |
---|
82 | end |
---|
83 | |
---|
84 | % What would the size be for a double |
---|
85 | dummy = reshape(X.basis(:,1),n,m); |
---|
86 | dummy = dummy(ind1); |
---|
87 | nnew = size(dummy,1); |
---|
88 | mnew = size(dummy,2); |
---|
89 | |
---|
90 | [nx,mx] = size(X.basis); |
---|
91 | % Sparse row-based subsref can be EEEEEEEXTREMELY SLOW IN SOME CASES |
---|
92 | % FIX : Smarter approach? |
---|
93 | % |
---|
94 | % try |
---|
95 | % [ix,jx,sx] = find(X.basis); |
---|
96 | % [keep ,loc] = ismember(ix,ind1);keep = find(keep); |
---|
97 | % ix = loc(keep);%ix = loc(ix); |
---|
98 | % jx = jx(keep); |
---|
99 | % sx = sx(keep); |
---|
100 | % Z = sparse(ix,jx,sx,length(ind1),mx); |
---|
101 | % catch |
---|
102 | % 9 |
---|
103 | % end |
---|
104 | |
---|
105 | if length(ind1) > 1 |
---|
106 | Z = X.basis.'; |
---|
107 | Z = Z(:,ind1); |
---|
108 | Z = Z.'; |
---|
109 | else |
---|
110 | Z = X.basis(ind1,:); |
---|
111 | end |
---|
112 | |
---|
113 | % if ~isequal(Z,Z2) |
---|
114 | % error |
---|
115 | % end |
---|
116 | |
---|
117 | % Find non-zero basematrices |
---|
118 | nzZ = find(any(Z(:,2:end),1)); |
---|
119 | if ~isempty(nzZ) |
---|
120 | X.dim(1) = nnew; |
---|
121 | X.dim(2) = mnew; |
---|
122 | X.lmi_variables = X.lmi_variables(nzZ); |
---|
123 | X.basis = Z(:,[1 1+nzZ]); |
---|
124 | else |
---|
125 | bas = reshape(X.basis(:,1),n,m); |
---|
126 | X.dim(1) = nnew; |
---|
127 | X.dim(2) = mnew; |
---|
128 | X.lmi_variables = []; |
---|
129 | X.basis = reshape(bas(ind1),nnew*mnew,1); |
---|
130 | end |
---|
131 | %nzZ = find(any(Z,1)); |
---|
132 | % % A bit messy code to speed up things |
---|
133 | % if isempty(nzZ) |
---|
134 | % bas = reshape(X.basis(:,1),n,m); |
---|
135 | % X.dim(1) = nnew; |
---|
136 | % X.dim(2) = mnew; |
---|
137 | % X.lmi_variables = []; |
---|
138 | % X.basis = reshape(bas(ind1),nnew*mnew,1); |
---|
139 | % else |
---|
140 | % if nzZ(1) == 1 |
---|
141 | % |
---|
142 | % else |
---|
143 | % end |
---|
144 | % end |
---|
145 | |
---|
146 | function X = subsref2d(X,ind1,ind2) |
---|
147 | |
---|
148 | if ischar(ind1) |
---|
149 | ind1 = 1:X.dim(1); |
---|
150 | end |
---|
151 | if ischar(ind2) |
---|
152 | ind2 = 1:X.dim(2); |
---|
153 | end |
---|
154 | |
---|
155 | % Convert to linear indecicies |
---|
156 | if islogical(ind1) |
---|
157 | ind1 = double(find(ind1)); |
---|
158 | end |
---|
159 | |
---|
160 | % Convert to linear indecicies |
---|
161 | if islogical(ind2) |
---|
162 | ind2 = double(find(ind2)); |
---|
163 | end |
---|
164 | |
---|
165 | n = X.dim(1); |
---|
166 | m = X.dim(2); |
---|
167 | lind2 = length(ind2); |
---|
168 | lind1 = length(ind1); |
---|
169 | if lind2 == 1 |
---|
170 | ind1_ext = ind1(:); |
---|
171 | else |
---|
172 | ind1_ext = kron(repmat(1,lind2,1),ind1(:)); |
---|
173 | end |
---|
174 | if lind1 == 1 |
---|
175 | ind2_ext = ind2(:); |
---|
176 | else |
---|
177 | ind2_ext = kron(ind2(:),repmat(1,lind1,1)); |
---|
178 | end |
---|
179 | |
---|
180 | if prod(size(ind1_ext))==0 | prod(size(ind2_ext))==0 |
---|
181 | linear_index = []; |
---|
182 | else |
---|
183 | % Speed-up for some bizarre code with loads of indexing of vector |
---|
184 | if m==1 & ind2_ext==1 |
---|
185 | linear_index = ind1_ext; |
---|
186 | else |
---|
187 | linear_index = sub2ind([n m],ind1_ext,ind2_ext); |
---|
188 | end |
---|
189 | end |
---|
190 | nnew = length(ind1); |
---|
191 | mnew = length(ind2); |
---|
192 | |
---|
193 | % Put all matrices in vectors and extract sub matrix |
---|
194 | Z = X.basis(linear_index,:); |
---|
195 | % Find non-zero basematrices |
---|
196 | nzZ = find(any(Z(:,2:end),1)); |
---|
197 | if ~isempty(nzZ) |
---|
198 | X.dim(1) = nnew; |
---|
199 | X.dim(2) = mnew; |
---|
200 | X.lmi_variables = X.lmi_variables(nzZ); |
---|
201 | X.basis = Z(:,[1 1+nzZ]); |
---|
202 | else |
---|
203 | bas = reshape(X.basis(:,1),n,m); |
---|
204 | X.dim(1) = nnew; |
---|
205 | X.dim(2) = mnew; |
---|
206 | X.lmi_variables = []; |
---|
207 | X.basis = reshape(bas(linear_index),nnew*mnew,1); |
---|
208 | end |
---|