-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlin2ten.m
44 lines (38 loc) · 993 Bytes
/
lin2ten.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
function ten=lin2ten(lin,n,varargin)
% ten=lin2ten(lin,n,d) or ten=lin2ten(lin,n)
% ------------------------------------------
% Converts a linear index into a tensor multi-index, which counts from 1.
% The decomposition of the multi-index is specified by n and d.
%
% ten = vector, tensor multi-index,
%
% lin = scalar, linear index,
%
% n = vector, n(i) contains the ith dimension,
%
% d = scalar, only provided when n is a scalar argument. The
% size of the axis (=dimension) is then n^d.
%
% Reference
% ---------
%
% 11-2016, Kim Batselier, Ngai Wong
% if isscalar(n)
% d=varargin{1};
% n=n*ones(1,d);
% elseif isvector(n)
% d=length(n);
% end
d=length(n);
ten=zeros(1,d);
for i=d-1:-1:1
if rem(lin,prod(n(1:i)))==0
ten(i+1)=fix(lin/prod(n(1:i)));
lin=lin-(ten(i+1)-1)*prod(n(1:i));
else
ten(i+1)=fix(lin/prod(n(1:i)))+1;
lin=rem(lin,prod(n(1:i)));
end
end
ten(1)=lin;
end