0

I want to take a certain size of matrix(2 X 2 X 2) in overall matrix(whatever size), and want to check whether certain size of matrix (test matrix) has some elements(i.e. 1 or 2), however, I don't know how to check, easily. I just wrote the code as shown below. In my view, there are some way to be optimized. Please give me some helpful advice. Thanks!

def labeling(input_matrix)
    test_matrix = np.zeros((2,2,2))
    for i in input_matrix.shape[0]
        for j in input_matrix.shape[1]
            for k in input_matrix.shape[2]
                test_matrix[i,j,k] = input_matrix[i,j,k]
                test_matrix[i+1,j,k] = input_matrix[i+1,j,k]
                test_matrix[i,j+1,k] = input_matrix[i,j+1,k]
                test_matrix[i+1,j+1,k] = input_matrix[i+1,j+1,k]
                test_matrix[i,j,k+1] = input_matrix[i,j,k+1]
                test_matrix[i+1,j,k+1] = input_matrix[i+1,j,k+1]
                test_matrix[i,j+1,k+1] = input_matrix[i,j+1,k+1]
                test_matrix[i+1,j+1,k+1] = input_matrix[i+1,j+1,k+1]

For example, this is what I expected in 2D.

input field = [0 0 0;
               1 2 1;
               3 0 2]

check matrix in unit of 2 * 2.

1) [0 0;
    1 2]  there is no 1 & 2 & 3. -> pass

2) [0 0;
    2 1]  there is no 1 & 2 & 3. -> pass

3) [1 2;                                    [4 4;
    3 0]  there is 1 & 2 & 3 -> make this    4 4]

output_field = [0 0 0;
                4 4 1; 
                4 4 2]
Newbie0105
  • 119
  • 1
  • 12
  • Please give example inputs and outputs in order to make your question more clear. It is a confusing question. https://stackoverflow.com/help/mcve – Luke DeLuccia Feb 25 '19 at 18:24
  • Possible duplicate of [Slicing a matrix with Python](https://stackoverflow.com/questions/30980011/slicing-a-matrix-with-python) – miara Feb 25 '19 at 20:01

0 Answers0