subsref.m 2.42 KB
Newer Older
1 2 3 4 5 6 7
function B = subsref(self,idx)

% handle case with a single subscript
% for example CA(234)
if strcmp(idx.type,'()') && length(idx.subs) == 1
    % indices of elements in B
    ind = idx.subs{1};
8 9 10 11 12

    if strcmp(ind,':')
        ind = [1:numel(self)]';
    end

13 14 15 16 17
    % output array
    B = zeros(size(ind));
    B(:) = NaN;
    
    if self.overlap 
18
        % transform linear index ind to subscript subs
19 20 21 22 23 24 25 26 27 28 29 30 31
        subs = cell(1,self.nd);
        [subs{:}] = ind2sub(size(self),ind);
        
        % make a nice array length(ind) by self.nd
        subs = cell2mat(subs);
        
        
        % get every element
        idxe.type = '()';
        idxe.subs = cell(1,self.nd);
        
        for i=1:length(ind)
            idxe.subs = mat2cell(subs(i,:),1,ones(1,self.nd));
32
            B(i) = subsref_canonical(self,idxe);
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        end
    else
        % assume all arrays does not overlap
                
        for j=1:self.na
            sel = self.bounds(j) < ind & ind <= self.bounds(j+1);
            B(sel) = self.arrays{j}(ind(sel) - self.bounds(j));
        end
        
    end
elseif strcmp(idx.type,'.')
  % load attributes from first array
    B = subsref(self.arrays{1},idx);
else
    B = subsref_canonical(self,idx);
end

end

% for this function we assume that idx.subs has the same dimension than
% the array

function B = subsref_canonical(self,idx)
[idx_global,idx_local,sz] = idx_global_local_(self,idx);

B = zeros(sz);
B(:) = NaN;

for j=1:self.na
    % get subset from j-th array
    subset = subsref(self.arrays{j},idx_local{j});
    
    % set subset in global array B
    % this is slow, why?
    %B = subsasgn(B,idx_global{j},subset);
    
    % however this quite fast
    idxa = idx_global{j}.subs;   
    B(idxa{:}) = subset;
end

end


% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; If not, see <http://www.gnu.org/licenses/>.