How do I sum a list<> of arrays

后端 未结 7 1390
故里飘歌
故里飘歌 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:05

    I would do it as follows … but this solution might actually be very slow so you might want to run a benchmark before deploying it in performance-critical sections.

    var result = xs.Aggregate(
        (a, b) => Enumerable.Range(0, a.Length).Select(i => a[i] + b[i]).ToArray()
    );
    

提交回复
热议问题