# How to reduce large matrix to a multidimensional matrix?

I have a large matrix $$h$$ with dimension $$N_x N_y\times N_x N_y$$.

it is related to matrix $$a$$ by this formula,where $$i,i_1=1..N_x$$ and $$j,j_1=1..N_y$$.

hs[[ Nx (j - 1) + i]][[ Nx (j1 - 1) + i1]] = a[i, j, i1, j1];


i need to populate my matrix $$h$$ using this condition

a[i, j, i + 1, j] =  a[i, j, i , j + 1] = 1


How to make this efficiently, so that each time i called $$a$$ my $$h$$ changed?

I tried to do this

{Nx = 32, Ny = 16};

h = ConstantArray[0., {Nx Ny, Nx Ny}];

For[i = 1, i <= Nx , i++,
For[j = 1, j <= Ny, j++,
For[i1 = 1, i1 <= Nx , i1++,
For[j1 = 1, j1 <= Ny, j1++,
h[[Nx (j - 1) + i]][[ Nx (j1 - 1) + i1]] = a[i, j, i1, j1];
a[i, j, i1, j1] = 0;
]]]];


and then to change $$a$$ elements

For[i = 1, i < Nx , i++,
For[j = 1, j < Ny, j++,
a[i, j, i + 1, j] += 1;
a[i, j, i, j + 1] += 1;
]]


but it takes a lot of time to generate h

• From my quick look, it appears that you are mixing part "[[" and "]]" with single bracket. Is this intentional? Check out tutorial/TheFourKindsOfBracketingInTheWolframLanguage in the documentation section. Also, in your for loop, you have a variables "u" and "v" that don't seem to be defined and at that point are switching to using 6 indices rather than 2 or 4. Finally, a cut and paste of your For loop code shows mismatched brackets. – Mark R Jun 26 at 16:41
• thank you, i have edited my code.Should i use double brackets for a to get a better performance? – Alexander Nikolaenko Jun 26 at 17:10
• Double brackets are fundamentally different than single brackets. Go to the documentation (Help/WolframDocumentation) and type "brackets". Look there and you'll see what each of them mean. So it isn't a matter of performance, it is what you want to do. – Mark R Jun 26 at 18:20