'How to solve/what is the best way to approach it?

I have confusion/difficulties related to this code: the code didn't print out anything yet there is no error message. The context is, I want to change some components of the main random matrix (psi) to the new matrix (psiy) and I want to check whether my code is correctly with regard to the component's index (hence the print(init,sy)) but nothing comes out and no error message either. Anyone has any idea? Thank you very much in advance.

The full problem is here:

I have a 3D matrix (nx X ny X nz), with nx being the number of components in the x-axis (say, horizontal), ny being the number of components in the y-axis (say, vertical), and nz being the number of component in the z-axis (say, out of the plane). So the total of components is A. We can also see it as a 2x2 matrix with many layers (nz total of layers). The index of each component started from the top left of the first layer to the bottom right of the z-th layer.

So for a 4x4x4 matrix, we will have 0 to 63 indexes with 0 to 15 in the first layer, 16 to 31 in the second layer, and so on. And for the first layer, since nx and ny is 4, there are a total of 4 indexes in each row and column (0 to 3 for the first row, 4 to 7 for the second row, and 12 to 15 for the fourth row), the same order for the other layers.

I want to change my initial random matrix psi to a new matrix psiy with these conditions:

  1. The init-th components of the new matrix psiy will be the init-h components of the old matrix psi times 3
  2. The sy-th components of the new matrix psiy will be the sy-th components of the old matrix psi

Now, how do we know what are the init and sy components? We go back to the description of components of my 3D matrix, if we group it with respect to the column, for the first layer (index 0 to 15), there are 4 columns since ny is 4 and what I mean by init is the top index of the column, in this case the 0, 1, 2, and 3 indexes. So for the second layer init would be 16, 17, 18, and 19 indexes and so on for the rest of the layers.

While for sy, the definition is all the indexes in the even-th rows except for the last row. So for the case of nx, ny and nz is 4, sy indexes would be:

4 to 7 for the first layer (since there are only 4 rows (nx) and the only even-th row is the 2nd row (not including the last row)), and

20 to 23 for the second layer, 36 to 39 for the third layer, and 52 to 55 for the last layer.

So for ny is for example 8, the sy would be all the indexes in the 2nd, 4th, and 6th rows in each layer and so on for any (even) number of ny.

Thank you.

import numpy as np
nx=4
ny=4
nz=4
A=nx*ny*nz

HH=ny-2
H=int(HH/2)
psiy=np.zeros(A)
psi=np.random.randint(1,10,nx)

for i in range (0,nz):
    for m in range (1,H):
        for k in range (0,nx):
            init=i*nx*ny+k
            sy=init+(2*m-1)*nx
            psiy[init]=3*psi[init]
            psiy[sy]=1*psi[sy]
            print(init,sy)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source