02-14-2022 07:01 AM
I am collecting a group of 5 elements from a 1D array and do the sum of every group, then place the results of the sum into a 1D array. By looking at the VI attached, you'll know what I mean.
So my question is if I have to do the same thing indefinitely, what can I do to not keep using array subsets and sum blocks over and over? I do not want to use millions of "array subset" and "sum" blocks on my block diagram. I tried using shift registers and it did not work for me. I am stuck.
Can someone please help me?
Solved! Go to Solution.
02-14-2022 07:22 AM
Hi,
Please always make a stop condition in a While loop.
You want to do an action X times ==> it's time for a loop and here the for loop can be your friend
Just conceptualize what you have done to make it as a loop.
PS: I'm sure it can even be better than that.
02-14-2022 08:26 AM - edited 02-14-2022 08:36 AM
Unless I am gravely mistaken, there is a Collector on the pt by pt pallet.
Otherwise, use Quotient and Remainder( i+1, samples to sum) drive a case off Remainder (0 and Default cases) do nothing in default, in case Remainder = 0 resize array(samples to sum, Quotient) Sum the resulting 2D array.
Yes, defaphe, the sum array elements belongs after the for loop.
NOTE: That is not optimal! I can't see the OP's vi so I'm just not sure what exactly is desired or how many pts accrue each time the vi is called.
02-14-2022 09:48 AM - edited 02-14-2022 09:51 AM
Try something like this (You probably don't need the lower shift register and the indicator for the last five values, but they are useful for debugging):
Some quick notes:
I recommend to start with the learning resources listed on the top of the forum.
02-14-2022 09:59 AM
Thank you very much!!!! This solved my problem. Thanks for the advice!
02-14-2022 10:03 AM
Even simpler would be to just accumulate the sum of five in a scalar shift register.
02-14-2022 10:52 AM
Thanks Altenbach. You are awesome!
02-16-2022 05:40 AM
@altenbach wrote:
Try something like this (You probably don't need the lower shift register and the indicator for the last five values, but they are useful for debugging):
Some quick notes:
- Any toplevel while loop needs a wait. You don't want to spin it billions of times per second growing arrays forever.
- Building arrays indefinitely is never possible, because you'll run out of memory way before that.
- "Insert into array" is not the correct tool to build an array. Try "built array".
- Your lower shift register is the same as [i] and thus not needed.
- etc.
I recommend to start with the learning resources listed on the top of the forum.
I knew something about that example bothered me and I suggested using i++ as the dividend into Q&R for a reason. It actually took me a day to percolate a pot of coffee and engage brain:)
The case selector! You used cases for default and CONST:Divisor--. That assumes Divisor will never be turned into a control.
Its not often I catch you missing a trick. (Probably a bad planetary alignment)
02-16-2022 09:29 AM - edited 02-16-2022 09:32 AM
@JÞB wrote:
Its not often I catch you missing a trick. (Probably a bad planetary alignment)
I was fully aware that my code is not entirely scalable. I intentionally kept it simple so the OP does need to herd too many cats. :D.
(Possibilities are to use a boolean for the case structure that depends on a comparison of R and the size or we could do a [i]+1 before doing Q&R (because the order does not matter for averaging) and make the special case 0, which will be independent of the averaging size (This was actually how I wrote it first but I changed it because it might be confusing that the "array of five" does not fill in order). Real scalable code would allow changing the averaging length on the fly, requiring special handling for when it gets bigger or smaller. Let's not go down that rabbit hole. 😄 )
In any case, my second code (Scalar accumulator) is preferable and similar adjustments can be made there too).
02-16-2022 09:06 PM
Altenbach is the king of labview, labview master 💪