Transposing a static array

maik klein via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 20 06:47:58 PST 2016


On Saturday, 20 February 2016 at 03:02:11 UTC, cym13 wrote:
> On Saturday, 20 February 2016 at 02:26:56 UTC, maik klein wrote:
>> On Saturday, 20 February 2016 at 02:22:12 UTC, Ali Çehreli 
>> wrote:
>>> [...]
>>
>> Your "Method B" is how I did it too but how do I convert it 
>> back to a static array of float[2][3]?
>
> I don't see the point of using transposed which gives a range 
> out if you want to put it back into a float[2][3] right away, 
> just do a double foreach:
>
>     void main(string[] args) {
>         float[3][2] src = [[1, 2, 3], [2, 3, 4]];
>
>         float[2][3] dst;
>
>         foreach (i ; 0..src.length)
>             foreach (j ; 0..src[0].length)
>                 dst[j][i] = src[i][j];
>
>         assert(dst == [[1, 2], [2, 3], [3, 4]]);
>     }

You are right. I restricted myself from using indices because I 
wanted to learn ranges but I think that working with matrices is 
actually much simpler by just using good old for loops.


More information about the Digitalmars-d-learn mailing list