How do I sum a list<> of arrays

后端 未结 7 1394
故里飘歌
故里飘歌 2021-02-04 08:24

I have a List< int[] > myList, where I know that all the int[] arrays are the same length - for the sake of argument, let us say I have 500 arrays, each is 2048 elements long

7条回答
  •  南方客
    南方客 (楼主)
    2021-02-04 09:00

    OK, assuming we can assume that the sum of the ints at each position over the list of arrays will itself fit into an int (which is a dodgy assumption, but I'll make it anyway to make the job easier):

    int[] sums = 
        Enumerable.Range(0, listOfArrays[0].Length-1).
            Select(sumTotal => 
                Enumerable.Range(0, listOfArrays.Count-1).
                    Aggregate((total, listIndex) => 
                        total += listOfArrays[listIndex][sumTotal])).ToArray();
    

    EDIT - D'oh. For some reason .Select evaded me originally. That's a bit better. It's a slight hack because sumTotal is acting as both the input (the position in the array which is used in the Aggregate call) and the output sum in the resulting IEnumerable, which is counter-intuitive.

    Frankly this is far more horrible than doing it the old-fasioned way :-)

提交回复
热议问题