Search code examples
python-3.xnumpycube

Find matching rows in a numpy matrix of 3


Given a cube of mxmxm, I need to know the rows, in the 6 faces that the smallest value in their row is greater than a given n.


Solution

  • To obtain the various faces:

    faces = np.array([
        x[ 0,  :,  :],
        x[-1,  :,  :],
        x[ :,  0,  :],
        x[ :, -1,  :],
        x[ :,  :,  0],
        x[ :,  :, -1],
    ])
    

    Now collapse the last dimension axis:

    # No information on orientation provided by OP so always pick axis=-1
    row_mins = np.min(faces, axis=-1)
    

    And then keep only the rows that satisfy the condition:

    valid_rows = faces[row_mins > n]
    print(valid_rows)