## digitalmars.D.learn - Transposing a static array

• maik klein (5/5) Feb 19 2016 How would I transpose
• =?UTF-8?Q?Ali_=c3=87ehreli?= (25/30) Feb 19 2016 Because static arrays are not ranges, they must be used as slices with
• maik klein (3/39) Feb 19 2016 Your "Method B" is how I did it too but how do I convert it back
• cym13 (12/60) Feb 19 2016 I don't see the point of using transposed which gives a range out
• maik klein (4/22) Feb 20 2016 You are right. I restricted myself from using indices because I
• =?UTF-8?Q?Ali_=c3=87ehreli?= (14/33) Feb 19 2016 There is a problem there. Adding the following line
```How would I transpose

float[3][2]

to

float[2][3]

with http://dlang.org/phobos/std_range.html#.transposed
```
Feb 19 2016
=?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
```On 02/19/2016 06:00 PM, maik klein wrote:
How would I transpose

float[3][2]

to

float[2][3]

with http://dlang.org/phobos/std_range.html#.transposed

Because static arrays are not ranges, they must be used as slices with
the help of []. The following code does the same thing in two different
ways:

import std.stdio;
import std.range;
import std.algorithm;

void main() {
float[3][2] arr = [ [1, 2, 3],
[4, 5, 6] ];

// Method A
{
float[][] arr2;

foreach (ref a; arr) {
arr2 ~= a[];
}

writeln(arr2.transposed);
}

// Method B
{
auto r = arr[].map!((ref a) => a[]).array.transposed;
writeln(r);
}
}

Ali
```
Feb 19 2016
```On Saturday, 20 February 2016 at 02:22:12 UTC, Ali Çehreli wrote:
On 02/19/2016 06:00 PM, maik klein wrote:
How would I transpose

float[3][2]

to

float[2][3]

with http://dlang.org/phobos/std_range.html#.transposed

Because static arrays are not ranges, they must be used as
slices with the help of []. The following code does the same
thing in two different ways:

import std.stdio;
import std.range;
import std.algorithm;

void main() {
float[3][2] arr = [ [1, 2, 3],
[4, 5, 6] ];

// Method A
{
float[][] arr2;

foreach (ref a; arr) {
arr2 ~= a[];
}

writeln(arr2.transposed);
}

// Method B
{
auto r = arr[].map!((ref a) => a[]).array.transposed;
writeln(r);
}
}

Ali

Your "Method B" is how I did it too but how do I convert it back
to a static array of float[2][3]?
```
Feb 19 2016
cym13 <cpicard openmailbox.org> writes:
```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:
On 02/19/2016 06:00 PM, maik klein wrote:
How would I transpose

float[3][2]

to

float[2][3]

with http://dlang.org/phobos/std_range.html#.transposed

Because static arrays are not ranges, they must be used as
slices with the help of []. The following code does the same
thing in two different ways:

import std.stdio;
import std.range;
import std.algorithm;

void main() {
float[3][2] arr = [ [1, 2, 3],
[4, 5, 6] ];

// Method A
{
float[][] arr2;

foreach (ref a; arr) {
arr2 ~= a[];
}

writeln(arr2.transposed);
}

// Method B
{
auto r = arr[].map!((ref a) => a[]).array.transposed;
writeln(r);
}
}

Ali

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]]);
}
```
Feb 19 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.
```
Feb 20 2016
=?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
```On 02/19/2016 06:26 PM, maik klein wrote:
On Saturday, 20 February 2016 at 02:22:12 UTC, Ali Çehreli wrote:
On 02/19/2016 06:00 PM, maik klein wrote:
How would I transpose

float[3][2]

to

float[2][3]

with http://dlang.org/phobos/std_range.html#.transposed

auto r = arr[].map!((ref a) => a[]).array.transposed;

There is a problem there. Adding the following line

writeln(r.array);

causes an error:

core.exception.AssertError /usr/include/dmd/phobos/std/range
primitives.d(2207):
Attempting to fetch the front of an empty array of float

Adding the following two lines does *not* preserve the range (first one
does):

writeln(r.save);
writeln(r.save);

Did I not need (ref a) up there? I thought it would be a reference to a
float[3] element.

writeln(r);
}
}

Ali

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 know. :D

Ali
```
Feb 19 2016