Linear array to matrix

Boris Carvajal boris2.9 at gmail.com
Sat Apr 4 10:52:00 UTC 2020


On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria 
wrote:
> Hi.
> Is there a Built-in function (no code, only a built-in function)
> that transform a linear array to a Matrix?
>
> For example:
>
> From
>
> [10,20,30,40,50,60,70,80,90,100,110,120];
>
>
> To
>
> [
> [10,20,30],
> [40,50,60],
> [70,80,90],
> [100,110,120]
> ];
>
> Thank You very much
> Cheers.
> Giovanni

If you're really sure about the array and matrix dimensions/types.
You can use a cast:

     int[] a = [10,20,30,40,50,60,70,80,90,100,110,120];

     int[3][] m1 = cast(int[3][]) a;
     writeln(m1);

A better way is using the function chunks;

     import std.range;

     auto m2 = a.chunks(3);
     writeln(m2);



More information about the Digitalmars-d-learn mailing list