# Condensing indices in a matrix Classic List Threaded 5 messages Open this post in threaded view
|

## Condensing indices in a matrix

 Is it possible to construct a matrix of the following form without for loops?Start with a multidimensional array M( T1 , n1 , m1 , T2 , n2 , m2 )where we have the same size in the T1 and T2 dimensions, the n1 and n2 dimensions, and the m1 and m2 dimensions.Then, for each n and m, set M( 1 , n , m , 2 , n , m ) = a; M( 2 , n , m , 1 , n+1 , m ) = b; M( 2 , n , m , 1 , n , m+1 ) = c;for some numbers a, b and c. I can do this with e.g. M( 1 , : , : , 2 , : , : ) = a;so this is not a problem.Then condense the multidimensional array to a 2D array M( u(T1,n1,m1) , u(T2,n2,m2) ) where u is some function that condenses the indices from tuples of 3 to single indices. The function u can be anything that condenses our indices this way, though it'd be especially nice if I could define it myself.I am familiar with using reshape to condense all indices. Is there also a way to condense only certain indices? Or, is there another way to construct this matrix besides reshaping a multidimensional array?- Brett Green
Open this post in threaded view
|

## Re: Condensing indices in a matrix

 On 12/8/20 6:10 AM, Brett Green wrote: > Is it possible to construct a matrix of the following form without for > loops? > > Start with a multidimensional array > M( T1 , n1 , m1 , T2 , n2 , m2 ) > where we have the same size in the T1 and T2 dimensions, the n1 and n2 > dimensions, and the m1 and m2 dimensions. > > Then, for each n and m, set > M( 1 , n , m , 2 , n , m ) = a; > M( 2 , n , m , 1 , n+1 , m ) = b; > M( 2 , n , m , 1 , n , m+1 ) = c; > for some numbers a, b and c. I can do this with e.g. > M( 1 , : , : , 2 , : , : ) = a; > so this is not a problem. > > Then condense the multidimensional array to a 2D array > M( u(T1,n1,m1) , u(T2,n2,m2) ) > where u is some function that condenses the indices from tuples of 3 to > single indices. The function u can be anything that condenses our > indices this way, though it'd be especially nice if I could define it > myself. > > I am familiar with using reshape to condense all indices. Is there also > a way to condense only certain indices? Or, is there another way to > construct this matrix besides reshaping a multidimensional array? > > - Brett Green > Was it possible to send some minimal working example how array M is created?  For u(x,y,z), maybe `sub2ind`  can be helpful. HTH, Kai  https://octave.org/doc/v6.1.0/Advanced-Indexing.html
Open this post in threaded view
|

## Re: Condensing indices in a matrix

 On Tue, Dec 8, 2020 at 1:19 AM Kai Torben Ohlhus <[hidden email]> wrote: Was it possible to send some minimal working example how array M is created?  For u(x,y,z), maybe `sub2ind`  can be helpful. HTH, Kai  https://octave.org/doc/v6.1.0/Advanced-Indexing.html Thank you and please excuse the slow response. Here is an MWE:ind = @(T,n,m) 2*N*(m-1) + 2*(n-1) + T + 1;M = zeros(2*N^2, 2*N^2);for n=1:N for m=1:N M( ind(0,n,m) , ind(1,n,m) ) = 1; M( ind(1,n,m) , ind(0,n,m) ) = 1; M( ind(0,n,m) , ind(1,n+1,m) ) = 1; M( ind(1,n+1,m) , ind(0,n,m) ) = 1; M( ind(0,n,m) , ind(1,n,m+1) ) = 1; M( ind(1,n,m+1) , ind(0,n,m) ) = 1; endendWhile it's nice that sub2ind exists, I prefer to do the indexing myself so that it is explicitly readable from the code, and so that I can control it directly. The key problem for me is the assignment, which I would like to do without for loops if possible.