How to resolve the algorithm Matrix transposition step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Matrix transposition step by step in the D programming language

Table of Contents

Problem Statement

Transpose an arbitrarily sized rectangular Matrix.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Matrix transposition step by step in the D programming language

Source code in the d programming language

void main() {
    import std.stdio, std.range;

    /*immutable*/ auto M = [[10, 11, 12, 13],
                            [14, 15, 16, 17],
                            [18, 19, 20, 21]];
    writefln("%(%(%2d %)\n%)", M.transposed);
}


T[][] transpose(T)(in T[][] m) pure nothrow {
    auto r = new typeof(return)(m[0].length, m.length);
    foreach (immutable nr, const row; m)
        foreach (immutable nc, immutable c; row)
            r[nc][nr] = c;
    return r;
}

void main() {
    import std.stdio;

    immutable M = [[10, 11, 12, 13],
                   [14, 15, 16, 17],
                   [18, 19, 20, 21]];
    writefln("%(%(%2d %)\n%)", M.transpose);
}


import std.stdio, std.algorithm, std.range, std.functional;

auto transpose(T)(in T[][] m) pure nothrow {
    return m[0].length.iota.map!(curry!(transversal, m));
}

void main() {
    immutable M = [[10, 11, 12, 13],
                   [14, 15, 16, 17],
                   [18, 19, 20, 21]];
    writefln("%(%(%2d %)\n%)", M.transpose);
}


  

You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the 11l programming language
You may also check:How to resolve the algorithm Ordered words step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Ordered words step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Determine if only one instance is running step by step in the Julia programming language
You may also check:How to resolve the algorithm Metered concurrency step by step in the Oforth programming language