LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Sum of the elements in an array

Hi guys,

 

I would like to do this operation with the elements in my 1D array.

 

Given array:

a

b

c

d

 

what i would obtain in a new array:

a

a+b

a+b+c

a+b+c+d

 

could you show me how to do?

 

 

0 Kudos
Message 1 of 11
(4,192 Views)

I would something like this:

 

Example.png

Tim
GHSP
0 Kudos
Message 2 of 11
(4,186 Views)

Here's what I usually do

 

 

altenbach_0-1583771215171.png

 

0 Kudos
Message 3 of 11
(4,184 Views)

I use this, but save it as a VIM. (Note it will not catch overflow errors for integer types).

 

Snap30.png

 

mcduff

Message 4 of 11
(4,153 Views)

Well, since the question is a bit vague, here's an alternative interpretation 😄

 

(... and yes I know there are much more efficient ways ...;)

 

altenbach_0-1583780107369.png

 

(well, the subject says "sum", but who knows? ;))

 

 

Message 5 of 11
(4,126 Views)

terickson_0-1583785908457.png

 

CLA
Message 6 of 11
(4,082 Views)

Here's a method that operates completely in-place, which can be turned into a VIM also.

 

Snap31.png

 

 


@terickson wrote:

terickson_0-1583785908457.png

 


Neat implementation, but slow for big arrays.

 

mcduff

0 Kudos
Message 7 of 11
(4,064 Views)

@mcduff wrote:

Here's a method that operates completely in-place, which can be turned into a VIM also.

 

Snap31.png

 


Don't underestimate the compiler. Mine (above) has the same number of array buffer allocations. 😉

 

If you really want to use the IPE, I think having an extra scalar (below) probably compensates for your operating on two array elements with each iteration. Of course you have one fewer iterations, which could make a difference for extremely small arrays.

 

Not tested, just guessing of course. 😄

 

 

altenbach_0-1583797221647.png

 

0 Kudos
Message 8 of 11
(4,036 Views)

@altenbach wrote:


Don't underestimate the compiler. Mine (above) has the same number of array buffer allocations. 😉

 


+1. This is why I said the VI operates totally in-place. The screen shots below are LabVIEW 2019, using the "Profile Buffer Allocations Tool"

 

Profile Tool only records 1 bufferProfile Tool only records 1 buffer

 

Using the VI from the earlier post, which is a bit faster, I see this

Profile Tool records two buffersProfile Tool records two buffers

 

It's possible the first buffer in the second screen shot is erased immediately after execution; this is @rolfk territory, so in effect 1 buffer. But at least according to NI's tools, there are 2 buffer allocations.

 


@altenbach wrote:
If you really want to use the IPE, I think having an extra scalar (below) probably compensates for your operating on two array elements with each iteration. Of course you have one fewer iterations, which could make a difference for extremely small arrays.

 

Not tested, just guessing of course. 😄

 

 

altenbach_0-1583797221647.png

 

Besides complex numbers I thought you liked to do things in parallel. 🙂

 

mcduff


 

0 Kudos
Message 9 of 11
(4,028 Views)

I like these threads. Its like when someone asks a in forum to photoshop him holding the sun. (or, no its not quite there yet). 

Certified LabVIEW Architect
Message 10 of 11
(3,959 Views)