LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Sum of element one by one from 1D arrays

Hi,

I have 20 1D arrays in a file and each has 80 elements. What I want is the read and sum these arrays like:

 

first array   second array    third array        Summing array

    12                   10                  15                      37

     4                      6                    8                      18

     14                   10                   3                      27

 

I will be grateful if you could help me.

Bests.

 

0 Kudos
Message 1 of 11
(3,598 Views)

If you can load them all into memory and form a 2D array, you can take the Transpose and then pass them to a For Loop and use the Sum Array Elements node.

If you have them as separate 1D arrays, you can use a Compound Arithmetic node (CAr) and the add method.

 

If you can't, then put a shift register on a For loop and pass in all of your paths as an autoindexed input, and load the data for each one in turm, summing each element with the array stored in the shift register (with 80 elements at all times).

 

20*80 doesn't sound so big, so I'd suggest the first or second approachs as a better methods (depending on how the input is arranged):

Example_VI.png


GCentral
0 Kudos
Message 2 of 11
(3,542 Views)

Thanks for the answer, 

I have one more problem: The 1D data came as below, but it can be 2D also, so I can use both methods. So, what is your suggestion?

 

Thanks for the help.

Capture.PNG

0 Kudos
Message 3 of 11
(3,529 Views)

@Erdinc wrote:

I have one more problem: The 1D data came as below, but it can be 2D also, so I can use both methods. So, what is your suggestion?

 


 

It is either a 1D or a 2D array, it cannot change (Unless you use a malleable vim).

 

What problem are you trying to solve with the 1D array? (sum all elements of it? (for that you don't need a FOR loop), Something else?)

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

Hi,

The problem is I get 20 1D arrays with 80 elements in the loop. The above solution can be used in existing 1D arrays, but in my case, 20 1D arrays occur consecutively and I need to sum their elements as I mentioned above.

 

Thanks and bests.

0 Kudos
Message 5 of 11
(3,519 Views)

Earlier, you said they are "in a file", now you say they are "in the loop". Mixed messages....

 

Do you want a new 1D array which contains the sums of each 1D array from each iteration? In that case, just use "add array elements" and autoindex the resulting scalar at an output tunnel to make an array.

 

Can you boil it down to a simplified example and attach the VI? Then tell us exactly what you expect.

0 Kudos
Message 6 of 11
(3,510 Views)

Hi,

 

I attached a simple example of my situation. After each iteration, I get a 1D array that contains 80 elements. So I  want a new 1D array  which contains the sums of each 1D array from each iteration like:

 

first array     second array     third array    ...........    sum of arrays

   1                       5                      7           .............           13

   5                        8                     10         ..............          23

   10                     13                    11         ..............          34

 

Capture.PNG

Thanks and bests.

 

0 Kudos
Message 7 of 11
(3,504 Views)

You mean like this?

 

altenbach_0-1580589709247.png

0 Kudos
Message 8 of 11
(3,501 Views)

Unfortunately no. 

When I said the sum of the elements in the arrays, what I mean is that, for example, the sum of the first elements of all 1D arrays must be the first element of the new 1D array and so on, like below:

 

1. array  2. array   3. array      sum

   1             1             1              3

   1             2             3              6

 

In your solution, all elements of the first 1D array have been summed and saved as the first element of the new 1D array, and so on.

 

 

0 Kudos
Message 9 of 11
(3,483 Views)

2D arrays index by row and you show the sum of rows and that's what my solution did.

 

If you want to sum columns instead, do e..g as follows ...

 

altenbach_0-1580593852157.png

0 Kudos
Message 10 of 11
(3,476 Views)