0

I want to split a single 3D matrix into 3 smaller 3D matrices such that elements between any two planes are contained in a corresponding smaller matrix. These planes intersect at 120 degrees to each other at the center as given in the image. Let's say any arbitrary example in the figure I drew. AOPQRDA is one of the regions formed between the intersecting planes AORD and OPQR. There are 3 such regions in total. I want all the elements within each region contained in a separate matrix. Is it possible? The splitting is not based on the element value, but the index they are located in.

enter image description here

I started with any random 3D matrix since all it matters here is the index. We can choose any arbitrary center too. Those elements whose indexes meet the certain crietrion should be placed in one section. I haven't been able to come up with a logical planning. Any help is appreciated.Thank you

Rupesh
  • 103
  • 2
  • It turns often helpful in coming up with a planning to try to explain what is the background and the context of the question. What will the final outcome of the splitting be used for? – Claudio Jan 21 '23 at 20:55
  • Sure, I need to put a 3D object there, divide it into 3 sections calculate the values at each sections and integrate them – Rupesh Jan 22 '23 at 01:07
  • In other words you need a 2D-xy-matrix with same size as the 3D-xyz one filled with values telling to which of the three parts a single matrix element with given indices belong? – Claudio Jan 22 '23 at 07:33

2 Answers2

0

Below a suggestion for steps of a procedure able to accomplish the splitting (the logical planning):

  • create a white image with xy-size of the 3D-matrix
  • draw black one pixel wide lines (switch off anti-alias) splitting the image in three parts as required and fill the three parts with three different colors (using a bucket paint tool for filling an area with a color)
  • decide to which part does the black lines intersection pixel/point belong and change its color
  • decide to which parts do the lines belong and change their color (using a bucket paint tool)

Create an appropriate 2D-xy-matrix out of the image. The 2D-xy-matrix values tell for each pair of [x,y] indices to which part the 3D-xyz matrix the 3D [x,y,z] indices belong, so you can directly use this information for the calculations or for creation of 3D-xyz matrices out of the 2D-xy one to get a 3D-mask for each of the three parts you can use for vectorized way of calculations.

See here How to determine if a point is in a 2D triangle? for inspiration if you prefer to calculate the 2D-mask values from indices of the 2D-xy matrix given the point of intersection of the three lines dividing the area in three sections.

Claudio
  • 7,474
  • 3
  • 18
  • 48
  • Thank you, I divided regions in 2D keeping the zslice constant. I had some trouble setting the conditions but I guess it's done. – Rupesh Jan 23 '23 at 04:10
0

I think I managed to solve it:

import numpy as np
from scipy import ndimage
matrix=np.random.randint(0, 10, (5, 7, 7))
section1=[]
section2=[]
section3=[]
shape=matrix.shape
center_of_mass=[round(i) for i in ndimage.measurements.center_of_mass(matrix)][1::]
for z in range(shape[0]):
    for x in range(shape[1]):
        for y in range(shape[2]):
            if (x <=center_of_mass[0] and y <=center_of_mass[1]) or (x >center_of_mass[0] and y<=shape[2]-x-1):
                section1.append(matrix[z,x,y])
            if (x <=center_of_mass[0] and y >center_of_mass[1]) or (x >center_of_mass[0] and y>=x):
                section2.append(matrix[z,x,y])
            if (x >center_of_mass[0] and y>shape[2]-x-1) and (x >center_of_mass[0] and y<x) :
                section3.append(matrix[z,x,y])
[np.array(section1), np.array(section2) ,np.array(section3)]
Rupesh
  • 103
  • 2
  • Users find it difficult to understand code only answers with no explanation. Please add some description explaining what is does and how it solves the problem or add comments in the source code at appropriate places. – Azhar Khan Jan 24 '23 at 06:09
  • I have wrote a simplest code with variables describing the components. Sorry, but I don’t feel right in explaining if and else conditions or the for loop. Its obvious it takes a 3D list and divides elements based on the regions. The info id in the question too. – Rupesh Jan 25 '23 at 03:05