03-03-2015 07:16 AM
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])
03-03-2015 07:26 AM
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.
03-03-2015 07:36 AM
03-03-2015 08:37 AM - edited 03-03-2015 08:38 AM
Reshape Array works nicely here.
03-03-2015 11:18 AM
@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.).