Matrix transposition: Difference between revisions

m
whitespace/minor fixes
m (whitespace/minor fixes)
Line 3:
=={{header|ActionScript}}==
In ActionScript, multi-dimensional arrays are created by making an "Array of arrays" where each element is an array.
<lang ActionScript>function transpose( m:Array):Array
function transpose( m:Array):Array
{
//Assume each element in m is an array. (If this were production code, use typeof to be sure)
Line 27 ⟶ 26:
for(var i:uint = 0; i < M.length; i++)
trace(M[i]);</lang>
 
=={{header|Ada}}==
Transpose is a function of the standard Ada library provided for matrices built upon any floating-point or complex type. The relevant packages are Ada.Numerics.Generic_Real_Arrays and Ada.Numerics.Generic_Complex_Arrays, correspondingly.
Line 281:
 
return 0;
}</lang>output<lang>before transpose:
output
<pre>
/transposebefore {transpose:
1 2 3
4 5 6
Line 291 ⟶ 294:
1 4 7 10 13
2 5 8 11 14
3 6 9 12 15 </langpre>
 
=={{header|C++}}==
 
{{libheader|Boost.uBLAS}}
<lang cpp>#include <boost/numeric/ublas/matrix.hpp>
Line 317 ⟶ 319:
 
===Generic solution===
;main.cpp
 
main.cpp
<lang cpp>#include <iostream>
#include "matrix.h"
Line 358 ⟶ 359:
 
} /* main() */</lang>
;matrix.h
 
matrix.h
<lang cpp>#ifndef _MATRIX_H
#define _MATRIX_H
Line 567:
2 5
3 6</pre>
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Text;
Line 602 ⟶ 603:
 
=={{header|Clojure}}==
<lang lisp>(defmulti matrix-transpose
 
<lang lisp>
(defmulti matrix-transpose
"Switch rows with columns."
class)
Line 614 ⟶ 613:
(defmethod matrix-transpose clojure.lang.PersistentVector
[mtx]
(vec (apply map vector mtx)))</lang>
</lang>
 
Sample output:
<lang lisp>=> (matrix-transpose [[1 2 3] [4 5 6]])
 
[[1 4] [2 5] [3 6]]</lang>
<lang lisp>
=> (matrix-transpose [[1 2 3] [4 5 6]])
[[1 4] [2 5] [3 6]]
</lang>
 
=={{header|CoffeeScript}}==
 
<lang coffeescript>transpose = (matrix) ->
(t[i] for t in matrix) for i in [0...matrix[0].length]</lang>
Line 636 ⟶ 629:
 
=={{header|Common Lisp}}==
If the matrix is given as a list:<lang lisp>(defun transpose (m)
<lang lisp>(defun transpose (m)
(apply #'mapcar #'list m))</lang>
 
If the matrix A is given as a 2D array:
 
<lang lisp>;; Transpose a mxn matrix A to a nxm matrix B=A'.
(defun mtp (A)
Line 727 ⟶ 720:
=={{header|Factor}}==
<code>flip</code> can be used.
<lang factor>( scratchpad ) { { 1 2 3 } { 4 5 6 } } flip .
 
({ scratchpad{ )1 {4 } { 1 2 35 } { 4 53 6 } } flip .</lang>
{ { 1 4 } { 2 5 } { 3 6 } }
 
=={{header|Fortran}}==
Line 766 ⟶ 758:
20 CONTINUE
10 CONTINUE</lang>
 
=={{header|F_Sharp|F#}}==
Very straightforward solution...
<lang fsharp>let transpose (mtx : _ [,]) = Array2D.init (mtx.GetLength 1) (mtx.GetLength 0) (fun x y -> mtx.[y,x])</lang>
</lang>
 
=={{header|GAP}}==
Line 778 ⟶ 770:
[5, 25, 125, 625]];
transposedMatrix := TransposedMat(originalMatrix);</lang>
 
=={{header|Go}}==
===2D representation===
Line 945 ⟶ 938:
 
=={{header|Haskell}}==
 
For matrices represented as lists, there's ''transpose'':
 
<lang haskell>*Main> transpose [[1,2],[3,4],[5,6]]
[[1,3,5],[2,4,6]]</lang>
 
For matrices in arrays, one can use ''ixmap'':
 
<lang haskell>import Data.Array
 
Line 962 ⟶ 952:
 
=={{header|Hope}}==
<lang hope>uses lists;
 
<lang hope>
uses lists;
dec transpose : list (list alpha) -> list (list alpha);
--- transpose ([]::_) <= [];
--- transpose n <= map head n :: transpose (map tail n);</lang>
</lang>
 
=={{header|HicEst}}==
Line 988 ⟶ 975:
 
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>procedure transpose_matrix (matrix)
 
<lang Icon>
procedure transpose_matrix (matrix)
result := []
# for each column
Line 1,017 ⟶ 1,002:
write ("Transposed:")
print_matrix (transposed)
end</lang>
</lang>
 
Output:
<pre>
Line 1,032 ⟶ 1,015:
 
=={{header|IDL}}==
 
Standard IDL function <tt>transpose()</tt>
 
<lang idl>m=[[1,1,1,1],[2, 4, 8, 16],[3, 9,27, 81],[5, 25,125, 625]]
print,transpose(m)</lang>
Line 1,122 ⟶ 1,103:
=={{header|Joy}}==
For matrices represented as lists, there's ''transpose'', defined in seqlib like this:
<lang joy>DEFINE transpose == [ [null] [true] [[null] some] ifte ]
 
<lang joy>
DEFINE transpose == [ [null] [true] [[null] some] ifte ]
[ pop [] ]
[ [[first] map] [[rest] map] cleave ]
[ cons ]
linrec .</lang>
</lang>
 
=={{header|K}}==
Transpose is the monadic verb <code>+</code>
 
<lang k> {x^\:-1_ x}1+!:5
(1 1 1 1.0
Line 1,149 ⟶ 1,126:
=={{header|Liberty BASIC}}==
There is no native matrix capability. A set of functions is available at http://www.diga.me.uk/RCMatrixFuncs.bas implementing matrices of arbitrary dimension in a string format.
<lang lb>MatrixC$ ="4, 3, 0, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 1.00, 1.10"
<lang lb>
MatrixC$ ="4, 3, 0, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 1.00, 1.10"
 
print "Transpose of matrix"
Line 1,156 ⟶ 1,132:
print " ="
MatrixT$ =MatrixTranspose$( MatrixC$)
call DisplayMatrix MatrixT$</lang>
</lang>
 
Transpose of matrix<br>
Line 1,168 ⟶ 1,143:
| 0.10000 0.50000 0.90000 |<br>
| 0.20000 0.60000 1.00000 |<br>
| 0.30000 0.70000 1.10000 |<br>
 
 
=={{header|Lua}}==
Line 1,206 ⟶ 1,180:
=={{header|MATLAB}} / {{header|Octave}}==
Matlab contains two built-in methods of transposing a matrix: by using the "transpose(matrix)" function, or by using the " .' " operator. The " ' " operator yields the complex conjugate transpose.
 
<lang Matlab>>> transpose([1 2;3 4])
 
Line 1,247 ⟶ 1,220:
 
=={{header|OCaml}}==
 
Matrices can be represented in OCaml as a type <tt>'a array array</tt>, or using the module [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Bigarray.html Bigarray].
The implementation below uses a bigarray:
Line 1,302 ⟶ 1,274:
 
=={{header|Octave}}==
 
<lang octave>a = [ 1, 1, 1, 1 ;
2, 4, 8, 16 ;
Line 1,310 ⟶ 1,281:
tranposed = a.'; % tranpose
ctransp = a'; % conjugate transpose</lang>
 
 
=={{header|PARI/GP}}==
Line 1,351 ⟶ 1,321:
 
=={{header|Perl 6}}==
 
<lang perl6>sub transpose(@m)
{
Line 1,382 ⟶ 1,351:
 
.say for @a.&transpose;</lang>
Output:
 
Output:<pre>a f k p
b g l q
c h m r
Line 1,406 ⟶ 1,375:
 
=={{header|PL/I}}==
<lang PL/I>/* The short method: */
/* The short method: */
declare A(m, n) float, B (n,m) float defined (A(2sub, 1sub));
/* Any reference to B gives the transpose of matrix A. */</lang>
</lang>
Traditional method:
<lang PL/I>/* Transpose matrix A, result at B. */
<lang>
/* Transpose matrix A, result at B. */
transpose: procedure (a, b);
declare (a, b) (*,*) float controlled;
Line 1,427 ⟶ 1,393:
b(*,i) = a(i,*);
end;
end transpose;</lang>
</lang>
 
=={{header|Pop11}}==
 
<lang pop11>define transpose(m) -> res;
lvars bl = boundslist(m);
Line 1,449 ⟶ 1,413:
=={{header|PostScript}}==
{{libheader|initlib}}
<lang postscript>/transpose {
/transpose {
[ exch {
{ {empty? exch pop} map all?} {pop exit} ift
[ exch {} {uncons {exch cons} dip exch} fold counttomark 1 roll] uncons
} loop ] {reverse} map
}.</lang>
 
</lang>
 
=={{header|Prolog}}==
Predicate transpose/2 exists in libray clpfd of SWI-Prolog.<BR>
In Prolog, a matrix is a list of lists. transpose/2 can be written like that. <BR>
Works{{works with |SWI-Prolog.}}
 
<lang Prolog>% transposition of a rectangular matrix
% e.g. [[1,2,3,4], [5,6,7,8]]
Line 1,490 ⟶ 1,450:
 
% "quick" append
append_dl(X-Y, Y-Z, X-Z).</lang>
 
</lang>
 
=={{header|PureBasic}}==
Line 1,583 ⟶ 1,541:
 
=={{header|REXX}}==
<lang rexx>/*REXX program transposes a matrix, shows before and after matrixes. */
<lang rexx>
/*REXX program transposes a matrix, shows before and after matrixes. */
 
x.=''
Line 1,606 ⟶ 1,563:
call showMat 'B',cols,rows
exit
 
 
/*─────────────────────────────────────SHOWMAT subroutine───────────────*/
Line 1,614 ⟶ 1,570:
_=''; do c=1 for cols; _=_ right(value(mat'.'r'.'c),L); end; say _
end
return</lang>
</lang>
Output:
<pre>
<pre style="height:30ex;overflow:scroll">
---------------------------------A matrix---------------------------------
1.02 2.03 3.04 4.05 5.06 6.07 7.07
Line 1,633 ⟶ 1,588:
 
=={{header|RLaB}}==
<lang RLaB> >> m = rand(3,5)
>> m = rand(3,5)
0.41844289 0.476591435 0.75054022 0.226388925 0.963880314
0.91267171 0.941762397 0.464227895 0.693482786 0.203839405
Line 1,643 ⟶ 1,597:
0.75054022 0.464227895 0.26582235
0.226388925 0.693482786 0.11557427
0.963880314 0.203839405 0.0442493069</lang>
</lang>
 
=={{header|Ruby}}==
 
<lang ruby>m=[[1, 1, 1, 1],
[2, 4, 8, 16],
Line 1,852 ⟶ 1,804:
=={{header|Ursala}}==
Matrices are stored as lists of lists, and transposing them is a built in operation.
<lang Ursala>#cast %eLL
#cast %eLL
 
example =
Anonymous user