Filter löschen
Filter löschen

How to index a high dimensional matrix that grows exponentially

2 Ansichten (letzte 30 Tage)
Pooya Zeg
Pooya Zeg am 21 Jul. 2020
Bearbeitet: Bruno Luong am 22 Jul. 2020
I want to generate a high-dimensional matrix T (4^N by 4^N) using the elements of another matrix P (4^N by 16*N). My code can compute matrix P correctly for any N, but I cannot index T properly for any N. It looks there is a pattern that can help, but I am not able to find it. I calculated each element of matrix T one by one for N=2. I did the same for N=3 for 18 rows, but the calculations are too tedious. I attached my calculations for both cases. Any help would be appreciated!
I jsut realized that matrix P can be written in different ways because it only stores data. For example, it is a 512 by 1 matrix for N=2. Then, the first five rows of T are,
T(1,1) = P(1)*P(257)
T(1,2) = P(1)*P(257+16)
T(1,3) = P(1)*P(257+2*16)
T(1,4) = P(1)*P(257+3*16)
T(1,5) = P(17)*P(257)
T(1,6) = P(17)*P(257+16)
T(1,7) = P(17)*P(257+2*16)
T(1,8) = P(17)*P(257+3*16)
T(1,9) = P(33)*P(257)
T(1,10) = P(33)*P(257+16)
T(1,11) = P(33)*P(257+2*16)
T(1,12) = P(33)*P(257+3*16)
T(1,13) = P(49)*P(257)
T(1,14) = P(49)*P(257+16)
T(1,15) = P(49)*P(257+2*16)
T(1,16) = P(49)*P(257+3*16)
T(2,1) = P(2)*P(257+4*16+1)
T(2,2) = P(2)*P(257+5*16+1)
T(2,3) = P(2)*P(257+6*16+1)
T(2,4) = P(2)*P(257+7*16+1)
T(2,5) = P(18)*P(257+4*16+1)
T(2,6) = P(18)*P(257+5*16+1)
T(2,7) = P(18)*P(257+6*16+1)
T(2,8) = P(18)*P(257+7*16+1)
T(2,9) = P(34)*P(257+4*16+1)
T(2,10) = P(34)*P(257+5*16+1)
T(2,11) = P(34)*P(257+6*16+1)
T(2,12) = P(34)*P(257+7*16+1)
T(2,13) = P(50)*P(257+4*16+1)
T(2,14) = P(50)*P(257+5*16+1)
T(2,15) = P(50)*P(257+6*16+1)
T(2,16) = P(50)*P(257+7*16+1)
T(3,1) = P(3)*P(257+8*16+2)
T(3,2) = P(3)*P(257+9*16+2)
T(3,3) = P(3)*P(257+10*16+2)
T(3,4) = P(3)*P(257+11*16+2)
T(3,5) = P(19)*P(257+8*16+2)
T(3,6) = P(19)*P(257+9*16+2)
T(3,7) = P(19)*P(257+10*16+2)
T(3,8) = P(19)*P(257+11*16+2)
T(3,9) = P(35)*P(257+8*16+2)
T(3,10) = P(35)*P(257+9*16+2)
T(3,11) = P(35)*P(257+10*16+2)
T(3,12) = P(35)*P(257+11*16+2)
T(3,13) = P(51)*P(257+8*16+2)
T(3,14) = P(51)*P(257+9*16+2)
T(3,15) = P(51)*P(257+10*16+2)
T(3,16) = P(51)*P(257+11*16+2)
T(4,1) = P(4)*P(257+12*16+3)
T(4,2) = P(4)*P(257+13*16+3)
T(4,3) = P(4)*P(257+14*16+3)
T(4,4) = P(4)*P(257+15*16+3)
T(4,5) = P(20)*P(257+12*16+3)
T(4,6) = P(20)*P(257+13*16+3)
T(4,7) = P(20)*P(257+14*16+3)
T(4,8) = P(20)*P(257+15*16+3)
T(4,9) = P(36)*P(257+12*16+3)
T(4,10) = P(36)*P(257+13*16+3)
T(4,11) = P(36)*P(257+14*16+3)
T(4,12) = P(36)*P(257+15*16+3)
T(4,13) = P(52)*P(257+12*16+3)
T(4,14) = P(52)*P(257+13*16+3)
T(4,15) = P(52)*P(257+14*16+3)
T(4,16) = P(52)*P(257+15*16+3)
T(5,1) = P(69)*P(257)
T(5,2) = P(69)*P(257+16)
T(5,3) = P(69)*P(257+2*16)
T(5,4) = P(69)*P(257+3*16)
T(5,5) = P(85)*P(257)
T(5,6) = P(85)*P(257+16)
T(5,7) = P(85)*P(257+2*16)
T(5,8) = P(85)*P(257+3*16)
T(5,9) = P(101)*P(257)
T(5,10) = P(101)*P(257+16)
T(5,11) = P(101)*P(257+2*16)
T(5,12) = P(101)*P(257+3*16)
T(5,13) = P(117)*P(257)
T(5,14) = P(117)*P(257+16)
T(5,15) = P(117)*P(257+2*16)
T(5,16) = P(117)*P(257+3*16)
  1 Kommentar
Bruno Luong
Bruno Luong am 22 Jul. 2020
Bearbeitet: Bruno Luong am 22 Jul. 2020
"It looks there is a pattern that can help, but I am not able to find it."
Should you the one who should define how is the storage and derive appropriate indexing rather than "find it"?
It looks like if you index some vector with length 4^N is iquivalent to indexing an N-dimensional object (tensor?) where each dimention has 4 length:
T(4^N,4^N) ~ TT(4,4,4, ...) % 2N = N+N times on the RHS.
The indexing of the LHS array T and RHS multi-dimensional TT can be converted back and forth with sub2ind and ind2sub function.
T(i,j) ~ TT(i1,i2,...,iN,j1,j2,...,jN);
where
i = sub2ind([4,4,...4], i1,i2,...,iN);
[i1,i2,...,iN] = ind2sub([4,4,...4], i);
and similar for j.
If this is the case might be indexing in multi-dimensional array TT will be clearer for you (with the same kind of conversion for P).
Note that all the "..." of the above formal description can be effectively implemented in MATLAB syntax using cell/comma list of length N.
% As illustration for first dimmension of T only
% second dimension is supposed = 1
N = 6;
szT = [4^N,1];
% Generate random array
T = rand(szT);
% multi-dimensional
szTT = 4*ones(1,N); % [4 4 4 4 4 4]
TT = reshape(T, szTT);
% T = reshape(TT, szT)
Tindex = 2020 % example test linear index
ic = cell(1,N);
[ic{:}] = ind2sub(szTT,Tindex); % szTT is [4 4 4 4 4 4]
i = [ic{:}]; % [4 1 3 4 4 2] is this example
% Equivalent method to get i without using cell
% i = flip(dec2base(Tindex-1,4,N)-'0') + 1
% Get the element 2020 from T
Ti = T(Tindex)
% Get the same elemnt from TT using multi indexing (comma-list) array i
ic = num2cell(i);
TTi = TT(ic{:}) % TT(4,1,3,4,4,2) equal to T(2020)
% Check
isequal(Ti,TTi)
Note that back conversion from index vector i to linear index Tindex can be carried out like this
ic = num2cell(i);
Tindex = sub2ind(szTT,ic{:})
% or equivalently
Tindex = polyval(flip(i)-1,4)+1
% or
Tindex = base2dec(flip(i)+'0'-1,4)+1
My guess is if my formalism is suitable and once you sort out the equivalence, the tedious code can be written as few neat for-loop and array operations.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by