0

I have 2D array[,]. I want to multiple column one elements by column two by column three all possible combinations and put it to into new 1D array Lets say each column contains 4 elements so the finial array which contains multiplications of combinations all columns array size 4*4*4=64 (all combinations ).

ans[0]=array[0,0]*array[0,1]*array[0,2]
ans[1]=array[0,0]*array[0,1]*array[1,2]
ans[2]=array[0,0]*array[0,1]*array[2,2]
.
.
.
ans[64]=.....

It will be really appreciate if you can help this for me Thanks.

ram
  • 11
  • 2
    This is just a basic theoretical question. You can research on google. – Ruchi Sep 02 '15 at 16:52
  • @RuchiRahulDoshi that is harsh... – Adam Buchanan Smith Sep 02 '15 at 17:10
  • @AdamBuchananSmith I didnt mean to be harsh to the questioner, if any word seems like that, apologies!!!! – Ruchi Sep 02 '15 at 17:14
  • @RuchiRahulDoshi Sometimes googling something can be very difficult, especially if the OP does not know what they are searching for. If after the statement of researching it on google, you should of at least gave them something to go on like google search "this statement", otherwise it is not very constructive and remember this was his first post and a little nudge in the right direction won't hurt nothing. – Adam Buchanan Smith Sep 02 '15 at 17:19
  • 1
    I will keep that in mind in future of not being harsh and providing proper statement for the newbie to understand and work on his/her issue properly :) – Ruchi Sep 02 '15 at 18:31
  • 1
    @AdamBuchananSmith I think if you are looking for harsh statement you are the one making it - searching for title of own post is easy, but you imply that reading title of own post or entering it in search engine it is very hard for OP. :) – Alexei Levenkov Sep 02 '15 at 19:36

1 Answers1

1

For powers of 2 slicing number of all combinations into 3 sets of bits will give you all combinations of indexes. Sample for 4*4*4:

var result = Enumerable.Range(0,64)
  .Select(id => 
       array[(id & 0x18) >> 4, 0] *   // bits 4,5
       array[(id & 0x0C) >> 2, 1] *   // bits 2,3
       array[(id & 0x03) >> 0, 2])    // bits 0,1
  .ToArray();

Otherwise standard "get all combinations" approach will work - i.e. Combination Generator in Linq

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179