in reply to adding combinations of arrays (of arrays)

I've read your description and your modified code several times, and I'm still at a loss to understand what you are doing. And the output from your code does not clarify it for me.

A wordful description -- something more than "I need to return the sum of the elements of each combination of the arrays. " and maybe a worked example might clarify things and get you more useful responses.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

  • Comment on Re: adding combinations of arrays (of arrays)

Replies are listed 'Best First'.
Re^2: adding combinations of arrays (of arrays)
by aquinom (Sexton) on Nov 29, 2011 at 23:57 UTC
    Well I'm not sure how to more aptly say it, I'm sorry it's confusing for you. So if an array of arrays has 4 elements as in this example there are various combinations of these arrays that are possible depending on what your k is in n-choose-k. My example sums every combination in n-choose-2. So let's simplify it for the sake of expressing the problem: Array Of Arrays = 1,2,3,4 (each is an array) the six combinations choosing two at a time are 1+2,1+3,1+4,2+3,2+4,3+4 or six combinations (which is mathematically what 4 choose 2 equals). so if array 1 is 1,1,1,1 and array 2 is 1,1,0,0 the first SUMMED combination (i.e. 1+2) = 2,2,1,1 : this is how the outputs are created.

      That is much clearer. You want something like this?

      #! perl -slw use strict; use Data::Dump qw[ pp ]; use Algorithm::Combinatorics qw[ combinations ]; our $N //= 4; our $K //= 2; our $M //= 3; my @AoA = map{ [ map int( rand 2 ), 1 .. $M ] } 1 .. $N; pp 'Input:', \@AoA; my @newAoA; my $i = combinations( \@AoA, $K ); while( my $comb = $i->next ) { my @sums; for my $a ( @{ $comb } ) { $sums[ $_ ] += $a->[ $_ ] for 0 .. $#{ $a }; } push @newAoA, \@sums; } pp 'Output:', \@newAoA; __END__ c:\test>940693 ("Input:", [[0, 1, 1], [0, 1, 1], [1, 0, 1], [0, 1, 0]]) ( "Output:", [[0, 2, 2], [1, 1, 2], [0, 2, 1], [1, 1, 2], [0, 2, 1], [1, 1, 1]], ) c:\test>940693 -N=5 -M=2 -K=3 ("Input:", [[0, 1], [1, 0], [1, 1], [0, 1], [0, 0]]) ( "Output:", [ [2, 2], [1, 2], [1, 1], [1, 3], [1, 2], [0, 2], [2, 2], [2, 1], [1, 1], [1, 2], ], ) c:\test>940693 -N=5 -M=4 -K=3 ( "Input:", [ [1, 1, 1, 1], [0, 1, 1, 0], [1, 1, 0, 1], [1, 1, 1, 1], [0, 1, 1, 1], ], ) ( "Output:", [ [2, 3, 2, 2], [2, 3, 3, 2], [1, 3, 3, 2], [3, 3, 2, 3], [2, 3, 2, 3], [2, 3, 3, 3], [2, 3, 2, 2], [1, 3, 2, 2], [1, 3, 3, 2], [2, 3, 2, 3], ], )

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        Looks good, never seen some of that syntax you used to set the command line parameters. Thanks!