source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/opt/yalmip/extras/@ncvar/subsref.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 4.6 KB
Line 
1function 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)
8if  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   
21end
22
23if (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
29else
30    X = varargin{1};
31    Y = varargin{2};
32end
33
34try
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
54catch
55    error(lasterr)
56end
57if isempty(y.lmi_variables)
58        y = full(reshape(y.basis(:,1),y.dim(1),y.dim(2)));
59else
60     % Reset info about conic terms
61     y.conicinfo = [0 0];
62end
63varargout{1} = y;
64
65function X = subsref1d(X,ind1)
66
67% Get old and new size
68n = X.dim(1);
69m = X.dim(2);
70
71% Convert to linear indecicies
72if islogical(ind1)
73    ind1 = double(find(ind1));
74end
75
76% Ugly hack handle detect X(:)
77%pickall = 0;
78if ischar(ind1)
79    X.dim(1) = n*m;
80    X.dim(2) = 1;
81    return;
82end
83
84% What would the size be for a double
85dummy = reshape(X.basis(:,1),n,m);
86dummy = dummy(ind1);
87nnew = size(dummy,1);
88mnew = 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
105if length(ind1) > 1
106    Z = X.basis.';
107    Z = Z(:,ind1);
108    Z = Z.';
109else
110    Z = X.basis(ind1,:);
111end
112
113% if ~isequal(Z,Z2)
114%     error
115% end
116
117% Find non-zero basematrices
118nzZ = find(any(Z(:,2:end),1));
119if ~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]);
124else
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);
130end
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
146function X = subsref2d(X,ind1,ind2)
147
148if ischar(ind1)
149        ind1 = 1:X.dim(1);
150end
151if ischar(ind2)
152        ind2 = 1:X.dim(2);
153end
154
155% Convert to linear indecicies
156if islogical(ind1)
157    ind1 = double(find(ind1));
158end
159
160% Convert to linear indecicies
161if islogical(ind2)
162    ind2 = double(find(ind2));
163end
164
165n = X.dim(1);
166m = X.dim(2);
167lind2 = length(ind2);
168lind1 = length(ind1);
169if lind2 == 1
170    ind1_ext = ind1(:);
171else
172    ind1_ext = kron(repmat(1,lind2,1),ind1(:));
173end
174if lind1 == 1
175    ind2_ext = ind2(:);
176else
177    ind2_ext = kron(ind2(:),repmat(1,lind1,1));
178end
179
180if prod(size(ind1_ext))==0 | prod(size(ind2_ext))==0
181    linear_index = [];
182else
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
189end
190nnew = length(ind1);
191mnew = length(ind2);
192
193% Put all matrices in vectors and extract sub matrix
194Z = X.basis(linear_index,:);
195% Find non-zero basematrices 
196nzZ = find(any(Z(:,2:end),1));
197if ~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]);
202else
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);
208end
Note: See TracBrowser for help on using the repository browser.