-
Notifications
You must be signed in to change notification settings - Fork 8
/
track_moderankdect.m
executable file
·58 lines (54 loc) · 1.89 KB
/
track_moderankdect.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
function [rk rkt sap eigt err]=track_moderankdect(X,n,SamplingPara,Er,truerank)
%% mode rank detection based on SORTE using randomly sampled fibers
X=double(X);
Xdim=size(X);
NumOfMode=numel(Xdim);
DIM=prod(Xdim([1:n-1 n+1:end]));
if DIM<500
z=double(tenmat(tensor(X),n));
rk=sorte(z);
return;
end
p0=10;
NumOfSampled=10;
z=zeros(NumOfSampled,Xdim(n));
index=1:NumOfMode;index(n)=[];
var=cell(1,NumOfMode);
var{n}=1:Xdim(n);
track_it=1;
o=randperm(DIM);
curri=0;rk0=0;
for t=1:DIM
[var{index}]=ind2sub(Xdim([1:n-1 n+1:end]),o(t));
zt=reshape(double(X(var{:})),1,Xdim(n));
if norm(zt)>0.001
curri=curri+1;
z(curri,:)=zt;
end
fprintf('curri=%d *** \n',curri);
if ~rem(curri,SamplingPara)
[eigvect eigv]=eig(cov(z(1:curri,:)'));
[eigv ind]=sort(diag(eigv),'descend');
rk=sorte(eigv);
curri
rkt(track_it)=rk;
sap(track_it)=curri;
eigt{track_it}=eigv;
if curri<=truerank
err(track_it)=(norm(X-z'*(z'\X),'fro')-Er)/norm(X,'fro');
else
[uz dz]=eigs(cov(z'),truerank);
uz=uz'*z;
err(track_it)=(norm(X-uz'*(uz'\X),'fro')-Er)/norm(X,'fro');
end
% err(track_it)=norm(X-z'*inv(z*z')*z*X,'fro')/Er;
track_it=track_it+1;
if (curri>Xdim(n)-2*SamplingPara)
break;
else
rk0=rk;
end
end
end
fprintf('# rank=%d curr=%d\n',rk,curri);
end