Is there any function in R, Python, Lua, Java, Matlab or any programming language that can reduce the size of the 3D input_array from c(6, 6, 6) to c(4, 4, 4) by deleting all-zero matrices on the periphery of the 3 dimensions?
## Create an empty array with all zeros
input_array <- array(data = c(0), dim = c(6, 6, 6))
## Populate the central locations of the array with 1
input_array[, ,2:5] <- matrix(c(0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0),
nrow = 6, ncol = 6)
## Show the input_array
input_array
## The target output
output_array <- array(data = c(1), dim = c(4, 4, 4))
So, in other words, I am looking for a function that takes the input_array as an input and spits the output_array as an output. I want to maintain the 3D nature of the array throughout the conversion. The reason I am looking into this, is that I have very large 3D arrays with lots of zeros around the periphery and by removing all-zeros matrices out of the three dimensions, I can achieve considerable reduction in the sizes of these arrays and hence allow for more efficient processing.
In case there is no function out there, what could be a logic to write a new function so as to get this done? Use whatever language you prefer if you have anything to share, any feedback or help is very much appreciated.