Jump to content

Matrix transposition: Difference between revisions

added common lisp and ruby
(added common lisp and ruby)
Line 112:
PRINT
NEXT rows
 
=={{header|Common Lisp}}==
<lisp>(defun transpose (m)
(apply #'map #'list m))</lisp>
 
=={{header|D}}==
<pred>module mxtrans ;
import std.stdio ;
import std.string ;
Line 153 ⟶ 157:
n = m.transpose() ;
writefln(" n (m's transpose) = ", n.toString()) ;
}</pred>
 
 
Line 243 ⟶ 247:
 
=={{header|Java}}==
<java>import java.util.Arrays;
public class Transpose{
public static void main(String[] args){
double[][] m = {{1, 1, 1, 1},
{2, 4, 8, 16},
{3, 9, 27, 81},
{4, 16, 64, 256},
{5, 25, 125, 625}};
double[][] ans = new double[m[0].length][m.length];
for(int rows = 0; rows < m.length; rows++){
for(int cols = 0; cols < m[0].length; cols++){
ans[cols][rows] = m[rows][cols];
}
}
for(double[] i:ans){//2D arrays are arrays of arrays
System.out.println(Arrays.toString(i));
}
}
}</java>
}
 
=={{header|Mathematica}}==
Line 353 ⟶ 357:
=={{header|Perl}}==
{{libheader|Math::Matrix}}
<perl>use Math::Matrix;
 
$m = Math::Matrix->new(
[1, 1, 1, 1],
[2, 4, 8, 16],
[3, 9, 27, 81],
[4, 16, 64, 256],
[5, 25, 125, 625],
);
 
$m->transpose->print;</perl>
 
Output:
Line 391 ⟶ 395:
 
=={{header|Python}}==
<python>m=((1, #!/usr/bin/env python1, 1, 1),
m=( (12, 14, 18, 116),
(23, 49, 827, 1681),
(34, 916, 2764, 81256),
(45, 1625, 64125, 256625)),
print(zip(*m))</python>
(5, 25,125, 625));
print(zip(*m))</python>
Output:
[(1, 2, 3, 4, 5),
Line 403 ⟶ 406:
(1, 8, 27, 64, 125),
(1, 16, 81, 256, 625)]
 
=={{header|Ruby}}==
<ruby>m=[[1, 1, 1, 1],
[2, 4, 8, 16],
[3, 9, 27, 81],
[4, 16, 64, 256],
([5, 25,125, 625));]]
puts m.transpose</ruby>
Output:
[[1, 2, 3, 4, 5], [1, 4, 9, 16, 25], [1, 8, 27, 64, 125], [1, 16, 81, 256, 625]]
 
=={{header|Scheme}}==
<scheme>(define (transpose m)
(apply map list m))</scheme>
 
=={{header|TI-83 BASIC}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.