0
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Mark R Jun 26 at 16:41
  • $\begingroup$ thank you, i have edited my code.Should i use double brackets for a to get a better performance? $\endgroup$ – Alexander Nikolaenko Jun 26 at 17:10
  • $\begingroup$ 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. $\endgroup$ – Mark R Jun 26 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.