forked from andrewssobral/nway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
two2n.m
62 lines (53 loc) · 1.48 KB
/
two2n.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
59
60
61
function [IndicesN]=Two2N(DimX,Indices2);
%TWO2N Conversion of indices between unfoldings and N-way arrays
%
%
% function [IndicesN]=Two2N(DimX,Indices2);
%
%
% This algorithm requires access to:
% ''
%
% ---------------------------------------------------------
% Conversion of indices
% between unfoldings and N-way arrays
% ---------------------------------------------------------
%
% [IndicesN]=Two2N(DimX,Indices2);
%
% DimX : Dimensions of the N-way array.
% Indices2 : Indices in the unfolded 2-way array.
% indicesN : Indices in the N-way array
%
% This function helps you resolve the correct N-way indices
% to an entry in an unfolded array. If you e.g. want to know
% the N-way indices of the [3 240] element of a 5-way core
% with dimensions [7 6 5 8 7] you would write:
% two2n([7 6 5 8 7],[3 240]) and the answer is
% [3 6 5 8 1].
% Copyright (C) 1995-2006 Rasmus Bro & Claus Andersson
% Copenhagen University, DK-1958 Frederiksberg, Denmark, [email protected]
% $ Version 0.01 $ Date 11. July 1997 $ Not compiled $
C=size(DimX,2);
a=Indices2(1);
b=Indices2(2);
if a>DimX(1),
fprintf('two2n.m: The column index cannot be that high!\n');
return
end;
if b>prod(DimX)/DimX(1),
fprintf('two2n.m: The row index cannot be that high!\n');
return
end;
Tb=b;
Par(1)=a;
for c=C:-1:3,
factor=prod(DimX(2:c-1));
Par(c)=floor(Tb/factor)+1;
if (Tb-(Par(c)-1)*factor)<1,
Par(c)=Par(c)-1;
end;
Tb=Tb-(Par(c)-1)*factor;
end;
Par(2)=Tb;
IndicesN=Par;