11-13-2018 02:17 PM
Hi,
My final goal is to make a 3D convolution. I have one big 3D array (1000x1000x3) and one 3D small one (5x5x3) that I used to calculate the average value of value of 5x5x3 tiles in the big array.
My current strategy was to use a 2D convolution with my small 7x7 matrix and than make a mean in the 3rd direction. I block on how to make a mean in the 3rd direction. How do I do that?
Also, when I input a 3d array in a for loop, I know that it ''becomes'' a 2D array in the loop but I'm not in what sense does he slice the 3D array to make 2D arrays.
Also, keep in mind that I need this code to run as fast as possible.
Thank you for the help.
RMT
Solved! Go to Solution.
11-13-2018 02:42 PM - edited 11-13-2018 02:49 PM
Hi Raphael,
in what sense does he slice the 3D array to make 2D arrays.
3D array: pages, rows, columns
The first autoindexing tunnel will slice pages…
Also, keep in mind that I need this code to run as fast as possible.
Which code?
how to make a mean in the 3rd direction.
Use IndexArray and index the correct "direction", then apply Mean on the resulting subarray…
11-13-2018 05:16 PM
A page is the 3rd dimension. You can think of it like layers. If you start off with a 1000x1000x3 array and autoindex it with a for loop, the loop will iterate 3 times and the auto index tunnel will output a 1000x1000 subArray.
If you want to average all of the values of each "layer" for a particular row-column pair, you need to index the array using row/column values and leave the page entry blank.
For example, if you have a 3D array like below and you want to average row 0 and column 0 from each page, you would index row 0, column 0 and leave page value empty. The function will return a 1D array of 0,1,4. Average the values and autoindex them back into an array. Do this for all row/column pairs and you'll end up with a 2d array of averages for the entire array in the page direction.
Page 0 0 1 2 3 4 5 6 7 8 Page 1 1 1 1 2 2 2 3 3 3 Page 2 4 5 6
4 5 6 4 5 6
11-13-2018 07:05 PM
To average the l pages of a 3D array, autoindex on a For loop and add the pages using a shift register initialized to the correct size. At the end, divide the 2D array coming out of the shift register by N. No explicit indexing needed.
11-14-2018 09:19 AM
This is what I was explaining. I'm not sure what OP means by taking a mean in the 3rd direction but this was my interpretation of it. I'm only multiplying values here but that can be substituted with a mean function.
11-14-2018 10:06 AM - edited 11-14-2018 10:07 AM
I don't understand the multilpying. Are you trying a geometric average? Summing seems more appropriate.
In any case, these alternatives below do the same thing with one loop (One could use "initialize array" in the bottom code, requiring taking the correct sizes). Many other variations are possible:
Note that these are probably more efficient because frames are adjacent in memory while the elements of a Z columns are spread apart.
11-14-2018 10:12 AM
As I said in my post, I am multiplying to show the concept but it could be replaced with a mean function. I'm absolutely certain there are better ways of accomplishing what I coded but I do not know them.
@altenbach wrote:
I don't understand the multilpying. Are you trying a geometric average? Summing seems more appropriate.
11-14-2018 10:14 AM
That is the wrong direction. I don't want the mean of the row or column but of the depth. I want to go from a 1000x1000x3 matrix to a 1000x1000 one. The thing I don't like about previous exemples is the extensive use of loops. I'm use in Matlab to avoid loops. I fear that the process will be extensively slowed down by them.
11-14-2018 10:37 AM
@RaphaelMT wrote:
That is the wrong direction. I don't want the mean of the row or column but of the depth. I want to go from a 1000x1000x3 matrix to a 1000x1000 one. The thing I don't like about previous exemples is the extensive use of loops. I'm use in Matlab to avoid loops. I fear that the process will be extensively slowed down by them.
That is what my code does. It takes a 3d array and multiplies (or averages, as it were) in the 3rd dimension and returns a 2d array of those values. And assuming that Altenbach is right (he always is ), there are more efficient ways of doing this. However, AFAIK you won't be able to avoid loops. Matlab probably has a built in function to get a mean in a specific direction but the underlying code would almost assuredly be a loop.
If this is not clear to you or you still think that it's in the wrong direction, please post an example of a small dataset that shows the values you want to average and what you expect the output to be.
11-14-2018 10:43 AM
I learned two new functions. Thanks.