Matrix transposition: Difference between revisions

Simplified D code
(Added K Version)
(Simplified D code)
Line 621:
 
=={{header|D}}==
<lang d>import std.stdio, std.stringalgorithm, std.algorithmarray, std.arrayconv;
 
pure nothrow auto transpose(T)(in T[][] m) {
auto r = new T[][](m[0].length, m.length);
foreach (nr, row; m)
Line 629:
r[nc][nr] = c;
return r;
}
 
auto prettyPrint(T)(T[][] m) {
return "[" ~ array(map!format(m)).join("\n ") ~ "]";
}
 
void main() {
auto M = [[10, 11, 12, 13], [14,15,16,17], [18,19,20,21]];
[14, 15, 16, 17],
writeln("M:\n", M.prettyPrint());
[18, 19, 20, 21]];
writeln("\nM transposed:\n", M.transpose().prettyPrint());
auto Mt = transpose(M);
return writeln("[" ~, array(map!formattext(mMt)).join("\n ") ~, "]");
}</lang>
Output:
<pre>M:[[10, 14, 18]
[[10,11,12 15,13 19]
[14,1512, 16,17 20]
[1813,19,20 17, 21]]</pre>
 
M transposed:
[[10,14,18]
[11,15,19]
[12,16,20]
[13,17,21]]</pre>
 
=={{header|ELLA}}==
Anonymous user