LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to sum bits in array and put it in another array

how to sum bits in array and put it in another array like i want to sum first three bits in input array and put the sum of them in the first index of the output array and sum the second three bits in the input array and put the sum in the second index of output array like (input array [ 1 4 2 3 6 7  2  4  5 ] ....... output array [7 16  11])

0 Kudos
Message 1 of 5
(2,779 Views)

You want to sum sets of elements in an array.

 

The following code snippet uses two for loops to do what you want.  The first loop gets each progressive subset of 3 elements.  The inner loop sums the three elements together.  Finally, the sums are indexed together into an output array.

 

 

Sum_Every_3_Elements.png

 

 



0 Kudos
Message 2 of 5
(2,770 Views)

Array sum.png

0 Kudos
Message 3 of 5
(2,762 Views)

Reshape Array works nicely here.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 4 of 5
(2,745 Views)

@hussamwakeel wrote:

how to sum bits in array and put it in another array like i want to sum first three bits in input array and put the sum of them in the first index of the output array and sum the second three bits in the input array and put the sum in the second index of output array like (input array [ 1 4 2 3 6 7  2  4  5 ] ....... output array [7 16  11])


What does any of this have to do with "bits"?

Is the size of the input array always divisible by 3?

If not, should the end be truncated or padded?

 

I would probably go with tim's (crossrulz) solutions if you want to sum elements, but there are many other ways. (but e..g. the solution higher up with the while loop is very inefficient: autoindexing on a while loop, constant resizing of array in shift register, constant size checking, etc.).

0 Kudos
Message 5 of 5
(2,726 Views)