12-08-2009 02:16 AM
Hello,
I have a 2D Array. The code below generates a random 3x3 Array.
I want to average (or obtain the mean) of only the values above a defined value (user defined) in the array.
Could someone help me.
I've tried to use a "greater than" type case statement, but when I do that, for elements below the defined value a zero is placed into a new array. This alters the the average as it includes the zero as an element.
I almost need to create a new 1D array of just elements above the define value from the 3x3 array.
I wish to adapt from what I learn here to much larger arrays 400x400 and up.
Any help would be much appreciated.
Thanks
K
Solved! Go to Solution.
12-08-2009 03:28 AM
See attached example. Note that this algorithm is "brute force" and not optimized for speed or memory usage.
It simply iterates through the 2D array and builds a new 1D array with values above the threshold. This array is then used for analysis.
hope this helps,
Norbert
12-08-2009 04:19 AM
12-08-2009 05:52 AM - edited 12-08-2009 05:55 AM
Hi,
I had prepared this earlier but didnt have time to post before going to a meeting. The works done so here it is anyway.
There is a way to do it using array arithmetic so it should be pretty quick for you larger array.
However, I agree with Gerd's method as you could other functions like StDev if you need it
cheers
David
PS: I just noticed that you could using the Coerced? output to get the number of elements below threshold instead of the >= threshold code at the bottom.
12-08-2009 06:14 AM
Hi David,
as the OP said "much larger arrays" I prefer solutions which are inplace-ready
Your solution will create array copies (coerce, >=, boolean to number) with possible problems later on...
12-08-2009 06:40 AM
Gerd,
i don't get what you intend with this answer. The reason are the following issues:
a) standard mean function only works with 1D arrays (or am i mistaken here?)
b) as far as i know, the "standard" mean also sums up the whole array. Summing up an array containing an NaN will always result NaN as well. What is the mean of NaN??
Please don't take this post as an offence, i just wondered about your answer and tried some things the attached VI shows....
Norbert
12-08-2009 06:53 AM
Hi Norbert,
I must apologize, should check my messages before posting...
I thought Mean.vi would also take 2D arrays - seems I made a wrapper vi long time ago for this use case.
I also thought Mean.vi would omit NaN values when calculating the average - second fault...
12-08-2009 02:50 PM
Hello,
David Crawford - Thank you for your code. I do not wish to change the values to the coerced value. However, you have introduced me to another function block that I was unaware to and thank you for that.
Unfortunately , I am using LabVIEW 8.6. Please Norbert B, could you please repost the vi.
Thank You
Kamilan
12-09-2009 01:50 AM
Attached in LV 8.6. Please note that i did not downconvert the other example since it is imho to no avail for you.
Norbert