📄 subsref.m
字号:
function y = subsref(X,Y)
%SUBSREF (overloaded)
% Author Johan L鰂berg
% $Id: subsref.m,v 1.8 2005/03/02 17:18:38 johanl Exp $
try
switch Y.type
case '()'
% Check for simple cases to speed things up (yes, ugly but we all want speed don't we!)
switch size(Y.subs,2)
case 1
y = subsref1d(X,Y.subs{1});
case 2
y = subsref2d(X,Y.subs{1},Y.subs{2});
otherwise
error('Indexation error.');
end
otherwise
error(['Indexation with ''' Y.type ''' not supported']) ;
end
catch
error(lasterr)
end
if isempty(y.lmi_variables)
y = full(reshape(y.basis(:,1),y.n,y.m));
end
function X = subsref1d(X,ind1)
% Get old and new size
n = X.n;
m = X.m;
% Convert to linear indecicies
if islogical(ind1)
ind1 = double(find(ind1));
end
% Ugly hack handle detect X(:)
pickall=0;
if ischar(ind1)
ind1 = (1:n*m);
pickall=1;
end
% What would the size be for a double
dummy = reshape(X.basis(:,1),n,m);
dummy = dummy(ind1);
nnew = size(dummy,1);
mnew = size(dummy,2);
[nx,mx] = size(X.basis);
% Sparse row-based subsref can be EEEEEEEXTREMELY SLOW IN SOME CASES
% FIX : Smarter approach?
Z = X.basis';
Z = Z(:,ind1);
Z = Z';
% Find non-zero basematrices
nzZ = find(any(Z(:,2:end),1));
if ~isempty(nzZ)
X.n = nnew;
X.m = mnew;
X.lmi_variables = X.lmi_variables(nzZ);
X.basis = Z(:,[1 1+nzZ]);
else
bas = reshape(X.basis(:,1),n,m);
X.n = nnew;
X.m = mnew;
X.lmi_variables = [];
X.basis = reshape(bas(ind1),nnew*mnew,1);
end
% Fix...
if pickall
X.n = n*m;
X.m = 1;
end
function X = subsref2d(X,ind1,ind2)
if ischar(ind1)
ind1 = 1:X.n;
end
if ischar(ind2)
ind2 = 1:X.m;
end
% Convert to linear indecicies
if islogical(ind1)
ind1 = double(find(ind1));
end
% Convert to linear indecicies
if islogical(ind2)
ind2 = double(find(ind2));
end
n = X.n;
m = X.m;
lind2 = length(ind2);
lind1 = length(ind1);
if lind2 == 1
ind1_ext = ind1(:);
else
ind1_ext = kron(repmat(1,lind2,1),ind1(:));
end
if lind1 == 1
ind2_ext = ind2(:);
else
ind2_ext = kron(ind2(:),repmat(1,lind1,1));
end
if prod(size(ind1_ext))==0 | prod(size(ind2_ext))==0
linear_index = [];
else
% Speed-up for some bizarre code with loads of indexing of vector
if m==1 & ind2_ext==1
linear_index = ind1_ext;
else
linear_index = sub2ind([n m],ind1_ext,ind2_ext);
end
end
nnew = length(ind1);
mnew = length(ind2);
% Put all matrices in vectors and extract sub matrix
Z = X.basis(linear_index,:);
% Find non-zero basematrices
nzZ = find(any(Z(:,2:end),1));
if ~isempty(nzZ)
X.n = nnew;
X.m = mnew;
X.lmi_variables = X.lmi_variables(nzZ);
X.basis = Z(:,[1 1+nzZ]);
else
bas = reshape(X.basis(:,1),n,m);
X.n = nnew;
X.m = mnew;
X.lmi_variables = [];
X.basis = reshape(bas(linear_index),nnew*mnew,1);
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -