02-01-2020 04:58 AM
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.
02-01-2020 10:58 AM - edited 02-01-2020 11:04 AM
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):
02-01-2020 12:33 PM
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.
02-01-2020 12:40 PM - edited 02-01-2020 12:41 PM
@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?)
02-01-2020 01:13 PM
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.
02-01-2020 02:00 PM
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.
02-01-2020 02:31 PM
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
Thanks and bests.
02-01-2020 02:42 PM
02-01-2020 03:09 PM
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.
02-01-2020 03:51 PM - edited 02-01-2020 03:53 PM
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 ...