0001 function [elem] = subsref(tt,s)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 switch s(1).type
0019 case '()'
0020
0021 d=tt.d; n=tt.n; ps=tt.ps; cr=tt.core; r=tt.r;
0022
0023 pp=s.subs;
0024 mn=numel(pp);
0025 if ( mn < d && mn > 1 || (mn == 1 && numel(pp{1}) < d) )
0026 error('Invalid number of indices specified: given %d, need %d \n',mn,d);
0027 end
0028 if ( mn == 1 )
0029 ind=pp{1};
0030 if ( isa(ind,'double') )
0031 for i=1:d
0032 pp{i}=ind(i);
0033 end
0034 else
0035 pp=ind;
0036 end
0037 end
0038 elem=tt_tensor;
0039 elem.r=r;
0040 elem.d=d;
0041 n0=zeros(d,1);
0042 for i=1:d
0043 ind=pp{i};
0044 if ( strcmp(ind, ':'))
0045 pp{i}=1:n(i);
0046 ind=pp{i};
0047 end
0048 n0(i)=numel(ind);
0049 end
0050 ps1=cumsum([1;n0.*r(1:d).*r(2:d+1)]);
0051 cr1=zeros(ps1(d+1)-1,1);
0052 for i=1:d
0053 ind=pp{i};
0054 crx=cr(ps(i):ps(i+1)-1);
0055 crx=reshape(crx,[r(i),n(i),r(i+1)]);
0056 crx=crx(:,ind,:);
0057 cr1(ps1(i):ps1(i+1)-1)=crx(:);
0058 end
0059 if ( prod(n0) == 1 )
0060 v=1;
0061 for i=1:d
0062 crx=cr1(ps1(i):ps1(i+1)-1); crx=reshape(crx,[r(i),r(i+1)]);
0063 v=v*crx;
0064 end
0065 elem=v;
0066 else
0067 elem.n=n0;
0068 elem.core=cr1;
0069 elem.ps=ps1;
0070
0071 end
0072 case '.'
0073 switch s(1).subs
0074 case 'r'
0075 elem = tt.r;
0076 if (numel(s)>1)
0077 s = s(2:end);
0078 elem = subsref(elem, s);
0079 end;
0080
0081
0082
0083
0084
0085 case 'core'
0086 if (numel(s)>1)&&(strcmp(s(2).type,'()'))
0087 elem = tt.core(s(2).subs{1});
0088 else
0089 elem=tt.core;
0090 end;
0091 case 'd'
0092 elem=tt.d;
0093 case 'ps'
0094 elem = tt.ps;
0095 if (numel(s)>1)
0096 s = s(2:end);
0097 elem = subsref(elem, s);
0098 end;
0099
0100
0101
0102
0103
0104
0105 case 'n'
0106 elem = tt.n;
0107 if (numel(s)>1)
0108 s = s(2:end);
0109 elem = subsref(elem, s);
0110 end;
0111
0112
0113
0114
0115
0116
0117 otherwise
0118 error(['No field ', s.subs, ' is here.']);
0119 end
0120 case '{}'
0121
0122 pp=s.subs;
0123 mn=numel(pp);
0124 if ( mn > 1 )
0125 error('Invalid number of cores asked');
0126 end
0127 elem=core(tt,pp{1});
0128
0129 if (numel(s)>1)
0130 s = s(2:end);
0131 elem = subsref(elem, s);
0132 end;
0133
0134
0135
0136
0137 otherwise
0138 error('Invalid subsref.');
0139 end