How to sum multidimensional arrays?

Andrea Fontana nospam at example.com
Thu Feb 27 16:55:23 UTC 2020


On Thursday, 27 February 2020 at 14:15:26 UTC, p.shkadzko wrote:
> void main() {
>     int[][] m1 = rndMatrix(10, 2, 3);
>     int[][] m2 = rndMatrix(10, 2, 3);
>
>     auto c = m1[] + m2[];
> }


I think you're trying to do this:

int[][] m1 = rndMatrix(10, 2, 3);
int[][] m2 = rndMatrix(10, 2, 3);
int[][] m3;

m3.length = m1.length;
foreach(i; 0..m1.length)
{
     m3[i].length = m1[i].length;
     m3[i][] = m1[i][] + m2[i][];
}

But of course that's not the best solution :)


More information about the Digitalmars-d-learn mailing list