LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Mean on 3d Array in 3rd dimension and 3d arrays indexing in for loops

Solved!
Go to solution

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

0 Kudos
Message 1 of 19
(4,057 Views)

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…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 19
(4,041 Views)

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
aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 3 of 19
(4,024 Views)
Solution
Accepted by topic author RaphaelMT

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.

0 Kudos
Message 4 of 19
(4,017 Views)

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. 

Example_VI_BD.png

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 5 of 19
(4,005 Views)

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:

 

 

AverageFrames.png

 

Note that these are probably more efficient because frames are adjacent in memory while the elements of a Z columns are spread apart.

Message 6 of 19
(4,000 Views)

@altenbach wrote:

I don't understand the multilpying. Are you trying a geometric average? Summing seems more appropriate.

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.
aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 7 of 19
(3,997 Views)

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.

0 Kudos
Message 8 of 19
(3,994 Views)

@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 Smiley Wink), 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.

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 9 of 19
(3,985 Views)

I learned two new functions. Thanks.

0 Kudos
Message 10 of 19
(3,981 Views)