Matrix transposition: Difference between revisions
Content deleted Content added
mNo edit summary |
Not a robot (talk | contribs) add SETL |
||
(17 intermediate revisions by 12 users not shown) | |||
Line 3:
<br><br>
=={{header|11l}}==
<
V toRet = [[0] * matrix.len] * matrix[0].len
L(row) (0 .< matrix.len)
Line 14:
print(m)
print("After Transposition")
print(transpose(&m))</
{{out}}
<pre>
Line 23:
</pre>
=={{header|360 Assembly}}==
<
KN EQU 3
KM EQU 5
Line 61:
B LOOPI next i
ELOOPI EQU * out of loop i
...</
=={{header|68000 Assembly}}==
<
;INPUT:
;A0 = POINTER TO SOURCE ARRAY
Line 92:
MOVEM.L (SP)+,D2-D7
RTS</
=={{header|ACL2}}==
<
(if (or (endp xs) (endp xss))
nil
Line 111:
(list-each (first xss))
(cons-each (first xss)
(transpose-list (rest xss)))))</
=={{header|Action!}}==
<
TYPE Matrix=[
Line 180:
PutE() PrintE("Transpose:")
PrintMatrix(out)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Matrix_transposition.png Screenshot from Atari 8-bit computer]
Line 203:
=={{header|ActionScript}}==
In ActionScript, multi-dimensional arrays are created by making an "Array of arrays" where each element is an array.
<
{
//Assume each element in m is an array. (If this were production code, use typeof to be sure)
Line 225:
var M:Array = transpose(m);
for(var i:uint = 0; i < M.length; i++)
trace(M[i]);</
=={{header|Ada}}==
Line 231:
This example illustrates use of Transpose for the matrices built upon the standard type Float:
<
with Ada.Text_IO; use Ada.Text_IO;
Line 257:
Put_Line ("After Transposition:");
Put (Transpose (Matrix));
end Matrix_Transpose;</
{{out}}
<pre>
Line 273:
=={{header|Agda}}==
<
open import Data.Nat
Line 286:
a = (1 ∷ 2 ∷ 3 ∷ []) ∷ (4 ∷ 5 ∷ 6 ∷ []) ∷ []
b = transpose a</
'''b''' evaluates to the following normal form:
<
=={{header|ALGOL 68}}==
Line 297:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}}
<
[,]REAL m=((1, 1, 1, 1),
Line 323:
printf(($x"Transpose:"l$));
pprint((ZIP m))
)</
{{out}}
<pre>
Line 334:
</pre>
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="amazing hopper">
#include<hopper.h>
#proto showarraydata(_X_)
Line 367:
{"\nDIMENSION = ",nDims,"; ROWS = ",nRows,"; COLS = ",nCols,"\n"}, println
back
</syntaxhighlight>
{{out}}
<pre>
Line 400:
=={{header|APL}}==
If M is a matrix, ⍉M is its transpose. For example,
<
3 3⍴⍳10
1 2 3
Line 409:
2 5 8
3 6 9
</syntaxhighlight>
=={{header|AppleScript}}==
Line 415:
We can do this iteratively, by manually setting up two nested loops, and initialising iterators and empty lists,
<
transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
Line 435:
return lstTrans
end transpose</
Line 442:
{{trans|JavaScript}}
<
-- transpose :: [[a]] -> [[a]]
Line 462:
on run
transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
Line 469:
end run
-------------------- GENERIC FUNCTIONS -------------------
-- map :: (a -> b) -> [a] -> [b]
Line 493 ⟶ 494:
end script
end if
end mReturn</
{{Out}}
<
=={{header|Arturo}}==
<
X: size a
Y: size first a
Line 521 ⟶ 522:
loop arr 'row -> print row
print "-------------"
loop transpose arr 'row -> print row</
{{out}}
Line 536 ⟶ 537:
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">a = a
m = 10
n = 10
Line 594 ⟶ 595:
Return matrix
}
</syntaxhighlight>
===Using Objects===
<
R := []
for i, row in M
Line 602 ⟶ 603:
R[j,i] := col
return R
}</
Examples:<
MsgBox % ""
. "Original Matrix :`n" Print(Matrix)
Line 613 ⟶ 614:
Res .= (A_Index=1?"":"`t") col (Mod(A_Index,M[1].MaxIndex())?"":"`n")
return Trim(Res,"`n")
}</
{{out}}
<pre>Original Matrix :
Line 628 ⟶ 629:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f MATRIX_TRANSPOSITION.AWK filename
{ if (NF > nf) {
Line 643 ⟶ 644:
exit(0)
}
</syntaxhighlight>
<p>input:</p>
<pre>
Line 658 ⟶ 659:
</pre>
===Using 2D-Arrays===
<
{
i = NR
Line 686 ⟶ 687:
function max(m, n) {
return m > n ? m : n
}</
<p><b>Input:</b></p>
<pre>
Line 731 ⟶ 732:
=={{header|BASIC256}}==
<
dim matriz= {{78,19,30,12,36}, {49,10,65,42,50}, {30,93,24,78,10}, {39,68,27,64,29}}
dim mtranspuesta(matriz[,?], matriz[?,])
Line 750 ⟶ 751:
print
next fila
end</
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
DIM matrix(3,4), transpose(4,3)
Line 776 ⟶ 777:
NEXT
PRINT
NEXT row%</
{{out}}
<pre>
Line 790 ⟶ 791:
36 50 10 29
</pre>
=={{header|BQN}}==
{{trans|APL}}
If M is a matrix, ⍉M is its transpose. For example,
<syntaxhighlight lang="bqn">
3‿3⥊↕9
┌─
╵ 0 1 2
3 4 5
6 7 8
┘
⍉3‿3⥊↕9
┌─
╵ 0 3 6
1 4 7
2 5 8
┘
</syntaxhighlight>
=={{header|Burlesque}}==
<
blsq ) {{78 19 30 12 36}{49 10 65 42 50}{30 93 24 78 10}{39 68 27 64 29}}tpsp
78 49 30 39
Line 800 ⟶ 820:
12 42 78 64
36 50 10 29
</syntaxhighlight>
=={{header|C}}==
Transpose a 2D double array.
<
void transpose(void *dest, void *src, int src_h, int src_w)
Line 828 ⟶ 848:
printf("%g%c", b[i][j], j == 2 ? '\n' : ' ');
return 0;
}</
Transpose a matrix of size w x h in place with only O(1) space and without moving any element more than once. See the [[wp:In-place_matrix_transposition|Wikipedia article]] for more information.
<
void transpose(double *m, int w, int h)
Line 880 ⟶ 900:
return 0;
}</
{{out}}
<pre>
Line 896 ⟶ 916:
=={{header|C sharp|C#}}==
<
using System.Text;
Line 927 ⟶ 947:
}
}
}</
=={{header|C++}}==
Line 933 ⟶ 953:
{{libheader|Boost.uBLAS}}
<
#include <boost/numeric/ublas/io.hpp>
Line 947 ⟶ 967:
std::cout << trans(m) << std::endl;
}</
{{out}}
Line 956 ⟶ 976:
===Generic solution===
;main.cpp
<
#include "matrix.h"
Line 994 ⟶ 1,014:
}
} /* main() */</
;matrix.h
<
#define _MATRIX_H
Line 1,190 ⟶ 1,210:
}
#endif /* _MATRIX_H */</
{{out}}
Line 1,205 ⟶ 1,225:
===Easy Mode===
<
int main(){
Line 1,240 ⟶ 1,260:
return 0;
}</
{{out}}
<pre>
Line 1,257 ⟶ 1,277:
=={{header|Clojure}}==
<
"Switch rows with columns."
class)
Line 1,268 ⟶ 1,288:
[mtx]
(apply mapv vector mtx))
</syntaxhighlight>
{{out}}
<pre>=> (matrix-transpose [[1 2 3] [4 5 6]])
Line 1,274 ⟶ 1,294:
=={{header|CoffeeScript}}==
<
(t[i] for t in matrix) for i in [0...matrix[0].length]</
{{out}}
<pre>
Line 1,285 ⟶ 1,305:
=={{header|Common Lisp}}==
If the matrix is given as a list:
<
(apply #'mapcar #'list m))</
If the matrix A is given as a 2D array:
<
(defun mtp (A)
(let* ((m (array-dimension A 0))
Line 1,298 ⟶ 1,318:
(setf (aref B j i)
(aref A i j))))
B))</
=={{header|D}}==
===Standard Version===
<
import std.stdio, std.range;
Line 1,309 ⟶ 1,329:
[18, 19, 20, 21]];
writefln("%(%(%2d %)\n%)", M.transposed);
}</
{{out}}
<pre>10 14 18
Line 1,317 ⟶ 1,337:
===Locally Procedural Style===
<
auto r = new typeof(return)(m[0].length, m.length);
foreach (immutable nr, const row; m)
Line 1,332 ⟶ 1,352:
[18, 19, 20, 21]];
writefln("%(%(%2d %)\n%)", M.transpose);
}</
Same output.
===Functional Style===
<
auto transpose(T)(in T[][] m) pure nothrow {
Line 1,347 ⟶ 1,367:
[18, 19, 20, 21]];
writefln("%(%(%2d %)\n%)", M.transpose);
}</
Same output.
=={{header|Delphi}}==
See [[#Pascal]];
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
proc transpose . m[][] .
len n[][] len m[1][]
for i to len n[][]
for j to len m[][]
n[i][] &= m[j][i]
.
.
swap n[][] m[][]
.
m[][] = [ [ 1 2 3 4 ] [ 5 6 7 8 ] [ 9 10 11 12 ] ]
print m[][]
print ""
transpose m[][]
print m[][]
</syntaxhighlight>
{{out}}
<pre>
[
[ 1 2 3 4 ]
[ 5 6 7 8 ]
[ 9 10 11 12 ]
]
[
[ 1 5 9 ]
[ 2 6 10 ]
[ 3 7 11 ]
[ 4 8 12 ]
]
</pre>
=={{header|EchoLisp}}==
<
(lib 'matrix)
Line 1,363 ⟶ 1,416:
0 2 4
1 3 5
</syntaxhighlight>
=={{header|EDSAC order code}}==
In these two programs the matrix elements are stored in consecutive memory locations, in row-major order (that is, the 1st row from left to right, then the 2nd row, etc). For simplicity, matrix elements are short values, each occupying one memory location. The programs could be modified so that each element occupies two memory locations, as in the EDSAC library subroutines for vectors and matrices.
===Create a new matrix===
<
[Demo of matrix transposition. Not in place, creates a new matrix.
EDSAC, Initial Orders 2.]
Line 1,481 ⟶ 1,534:
E12Z [enter at 12 (relative)]
PF [accumulator = 0 on entry]
</syntaxhighlight>
{{out}}
<pre>
Line 1,497 ⟶ 1,550:
{{trans|C}}
That's a neat C program after the complications on Wikipedia. The way EDSAC handles arrays makes it convenient to modify the second half of the program. In C, the updated value of "next" has to be parked in another variable until m[next] (old "next") has been assigned to. In EDSAC, the instruction that assigns to m[next] can be planted before "next" is updated, and won't be affected by that update. Once the EDSAC program has been modified in this way, the code to update "next" is the same in both halves of the program and can be taken out as a subroutine.
<
[Transpose a matrix in place. EDSAC, Initial Orders 2.]
..PZ [blank tape and terminator]
Line 1,587 ⟶ 1,640:
T10@A9@A8@U9@TFA23@A2FT23@A10@A2FG19@A3@T10FA4@T11FA5@T12FA6@T13F
A38@GMO1@O2@A42@GXA10FTFA11FT10FAFT11FA50@GMO@ZFE11ZPF
</syntaxhighlight>
{{out}}
<pre>
Line 1,605 ⟶ 1,658:
=={{header|Elixir}}==
<
[2, 4, 8, 16],
[3, 9, 27, 81],
Line 1,613 ⟶ 1,666:
transpose = fn(m)-> List.zip(m) |> Enum.map(&Tuple.to_list(&1)) end
IO.inspect transpose.(m)</
{{out}}
Line 1,623 ⟶ 1,676:
Sample originally from ftp://ftp.dra.hmg.gb/pub/ella (a now dead link) - Public release.
Code for matrix transpose hardware design verification:<
[INT i = 1..m] [INT j = 1..n] matrix[j][i].</
=={{header|Emacs Lisp}}==
Line 1,630 ⟶ 1,683:
{{libheader|cl-lib}}
<
(defun transpose (m)
Line 1,636 ⟶ 1,689:
;;test for transposition function
(transpose '((2 3 4 5) (3 5 6 9) (9 9 9 9)))</
{{out}}
Line 1,644 ⟶ 1,697:
(4 6 9)
(5 9 9))
</pre>
Implementation using seq library:
<syntaxhighlight lang="lisp">
(defun matrix-transposition (m)
(apply #'seq-mapn (append (list #'list) m)) )
(let ((m '(( 2 0 -5 -1)
(-3 -2 -4 7)
(-1 -3 0 -6))))
(message "%s" (matrix-transposition m)) )
</syntaxhighlight>
{{out}}
<pre>
((2 -3 -1) (0 -2 -3) (-5 -4 0) (-1 7 -6))
</pre>
Line 1,650 ⟶ 1,721:
A nice introduction http://langintro.com/erlang/article2/ which is much more explicit.
<
-module(transmatrix).
-export([trans/1,transL/1]).
Line 1,669 ⟶ 1,740:
transL([ [] | List] ) -> transL(List);
transL([]) -> [].
</syntaxhighlight>
{{out}}
Line 1,682 ⟶ 1,753:
=={{header|Euphoria}}==
<
sequence out
out = repeat(repeat(0,length(in)),length(in[1]))
Line 1,700 ⟶ 1,771:
}
? transpose(m)</
{{out}}
Line 1,783 ⟶ 1,854:
=={{header|F_Sharp|F#}}==
Very straightforward solution...
<
=={{header|Factor}}==
<code>flip</code> can be used.
<
{ { 1 4 } { 2 5 } { 3 6 } }</
=={{header|Fermat}}==
Matrix transpose is built in.
<
Array a[3,1]
[a]:=[(1,2,3)]
Line 1,798 ⟶ 1,869:
[a]
[b]
</syntaxhighlight>
{{out}}
<pre>
Line 1,810 ⟶ 1,881:
{{libheader|Forth Scientific Library}}
{{works with|gforth|0.7.9_20170308}}
<
S" fsl/dynmem.seq" REQUIRED
: F+! ( addr -- ) ( F: r -- ) DUP F@ F+ F! ;
Line 1,821 ⟶ 1,892:
a{{ 5 3 & b{{ transpose
3 5 b{{ }}fprint</
=={{header|Fortran}}==
In ISO Fortran 90 or later, use the TRANSPOSE intrinsic function:
<
real, dimension(n,m) :: a = reshape( (/ (i,i=1,n*m) /), (/ n, m /) )
real, dimension(m,n) :: b
Line 1,837 ⟶ 1,908:
do j = 1, m
print *, b(j,:)
end do</
In ANSI FORTRAN 77 with MIL-STD-1753 extensions or later, use nested structured DO loops:
<
DATA ((A(I,J),I=1,3),J=1,5) /1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15/
Line 1,847 ⟶ 1,918:
B(J,I) = A(I,J)
END DO
END DO</
In ANSI FORTRAN 66 or later, use nested labeled DO loops:
<
DATA ((A(I,J),I=1,3),J=1,5) /1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15/
Line 1,857 ⟶ 1,928:
B(J,I) = A(I,J)
20 CONTINUE
10 CONTINUE</
Explicit transposition via DO-loops was available from the start. Less obvious is that Fortran uses what is called "column major" order rather than "row major", which is to say that consecutive elements of the array are stored in memory with indices counting down the columns first, not along the rows. The above examples acknowledge this in the DATA statement with the <code>((A(row,col),row=1,3),col=1,5)</code> which could therefore be replaced with just <code>A</code>, however one could use <code>((A(row,col),col=1,5),row=1,3)</code> instead and the DATA values could be arranged so as to appear in the same layout as one expects for a matrix. Consider <
EQUIVALENCE (A,C) !Occupy the same storage.
DATA A/
Line 1,886 ⟶ 1,957:
1 FORMAT (5F6.1) !Five values per line.
2 FORMAT (3F6.1) !Three values per line.
END</
Output:
Line 1,922 ⟶ 1,993:
=={{header|FreeBASIC}}==
<
{49,10,65,42,50},_
{30,93,24,78,10},_
Line 1,944 ⟶ 2,015:
Print
Next fila
Sleep</
{{out}}
<pre>
Line 1,962 ⟶ 2,033:
=={{header|Frink}}==
The built-in array method <CODE>transpose</CODE> transposes a 2-dimensional array.
<
a = [[1,2,3],
[4,5,6],
[7,8,9]]
joinln[a.transpose[]]
</syntaxhighlight>
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Matrix_transposition}}
'''Solution'''
Matrix transposition is an intrsinec operation in Fōrmulæ, through the Transpose expression:
[[File:Fōrmulæ - Matrix transposition 01.png]]
[[File:Fōrmulæ - Matrix transposition 02.png]]
However, a matrix transposition can be coded:
[[File:Fōrmulæ - Matrix transposition 03.png]]
[[File:Fōrmulæ - Matrix transposition 04.png]]
[[File:Fōrmulæ - Matrix transposition 02.png]]
=={{header|GAP}}==
<
[2, 4, 8, 16],
[3, 9, 27, 81],
[4, 16, 64, 256],
[5, 25, 125, 625]];
transposedMatrix := TransposedMat(originalMatrix);</
=={{header|Go}}==
===Library gonum/mat===
<
import (
Line 2,003 ⟶ 2,086:
fmt.Println()
fmt.Println(mat.Formatted(m.T()))
}</
{{out}}
<pre>
Line 2,015 ⟶ 2,098:
===Library go.matrix===
<
import (
Line 2,033 ⟶ 2,116:
fmt.Println("transpose:")
fmt.Println(m)
}</
{{out}}
<pre>
Line 2,047 ⟶ 2,130:
===2D representation===
Go arrays and slices are only one-dimensional. The obvious way to represent two-dimensional arrays is with a slice of slices:
<
import "fmt"
Line 2,081 ⟶ 2,164:
}
return r
}</
{{out}}
<pre>[1 2 3]
Line 2,092 ⟶ 2,175:
A flat element representation with a stride is almost always better.
<
import "fmt"
Line 2,142 ⟶ 2,225:
}
return r
}</
{{out}}
<pre>
Line 2,157 ⟶ 2,240:
{{trans|C}}
Note representation is "flat," as above, but without the fluff of constructing from rows.
<
import "fmt"
Line 2,217 ⟶ 2,300:
}
m.stride = h
}</
Output same as above.
=={{header|Groovy}}==
The Groovy extensions to the List class provides a transpose method:
<
[ 5, 6, 7, 8 ] ]
Line 2,229 ⟶ 2,312:
def transpose = matrix.transpose()
transpose.each { println it }</
{{out}}
Line 2,242 ⟶ 2,325:
=={{header|Haskell}}==
For matrices represented as lists, there's ''transpose'':
<
[[1,3,5],[2,4,6]]</
For matrices in arrays, one can use ''ixmap'':
<
swap (x,y) = (y,x)
Line 2,252 ⟶ 2,335:
transpArray :: (Ix a, Ix b) => Array (a,b) e -> Array (b,a) e
transpArray a = ixmap (swap l, swap u) swap a where
(l,u) = bounds a</
Using ''zipWith'' assuming a matrix is a list of row lists:
<
tpose [ms] = [[m] | m <- ms]
tpose (ms:mss) = zipWith (:) ms (tpose mss)
</syntaxhighlight>
{{out}}
<pre>
Line 2,264 ⟶ 2,347:
[[1,4,7],[2,5,8],[3,6,9]]
</pre>
or, in terms of Data.Matrix:
<syntaxhighlight lang="haskell">import Data.Matrix
main :: IO ()
main = print matrix >> print (transpose matrix)
where
matrix = fromList 3 4 [1 ..]</syntaxhighlight>
{{Out}}
<pre>┌ ┐
│ 1 2 3 4 │
│ 5 6 7 8 │
│ 9 10 11 12 │
└ ┘
┌ ┐
│ 1 5 9 │
│ 2 6 10 │
│ 3 7 11 │
│ 4 8 12 │
└ ┘</pre>
===With Numeric.LinearAlgebra===
<
a :: Matrix I
Line 2,276 ⟶ 2,380:
main = do
print $ a
print $ tr a</
{{out}}
<pre>(3><2)
Line 2,287 ⟶ 2,391:
=={{header|Haxe}}==
<
static function main() {
var m = [ [1, 1, 1, 1],
Line 2,303 ⟶ 2,407:
for(a in aa) Sys.println(a);
}
}</
{{Out}}
<pre>[1,1,1,1]
Line 2,316 ⟶ 2,420:
=={{header|HicEst}}==
<
mtx = 1.1 * $
Line 2,322 ⟶ 2,426:
SOLVE(Matrix=mtx, Transpose=mtx)
WRITE() mtx</
{{out}}
<pre>1.1 2.2 3.3 4.4
Line 2,333 ⟶ 2,437:
=={{header|Hope}}==
<
dec transpose : list (list alpha) -> list (list alpha);
--- transpose ([]::_) <= [];
--- transpose n <= map head n :: transpose (map tail n);</
=={{header|Icon}} and {{header|Unicon}}==
<
result := []
# for each column
Line 2,366 ⟶ 2,470:
write ("Transposed:")
print_matrix (transposed)
end</
{{out}}
<pre>
Line 2,380 ⟶ 2,484:
=={{header|IDL}}==
Standard IDL function <tt>transpose()</tt>
<
print,transpose(m)</
=={{header|Idris}}==
<
[[1, 3, 5], [2, 4, 6]] : List (List Integer)</
=={{Header|Insitux}}==
<syntaxhighlight lang="insitux">
(var transpose2d @(... map vec))
(transpose2d [[1 1 1 1] [2 4 8 16] [3 9 27 81] [4 16 64 256] [5 25 125 625]])
</syntaxhighlight>
{{out}}
<pre>
[[1 2 3 4 5] [1 4 9 16 25] [1 8 27 64 125] [1 16 81 256 625]]
</pre>
=={{header|J}}==
Line 2,392 ⟶ 2,510:
'''Example:'''
<
1 1 1 1
2 4 8 16
Line 2,402 ⟶ 2,520:
1 4 9 16 25
1 8 27 64 125
1 16 81 256 625</
As an aside, note that <code>.</code> and <code>:</code> are token forming suffixes (if they immediately follow a token forming character, they are a part of the token). This usage is in analogy to the use of [[wp:Diacritic|diacritics]] in many languages. (If you want to use <code> :</code> or <code>.</code> as tokens by themselves you must precede them with a space - beware though that wiki rendering software may sometimes elide the preceding space in <nowiki><code> .</code></nowiki> contexts.)
=={{header|Java}}==
<
public class Transpose{
public static void main(String[] args){
Line 2,425 ⟶ 2,543:
}
}
}</
=={{header|JavaScript}}==
===ES5===
{{works with|SpiderMonkey}} for the <code>print()</code> function
<
this.mtx = ary
this.height = ary.length;
Line 2,458 ⟶ 2,576:
print(m);
print();
print(m.transpose());</
produces
Line 2,474 ⟶ 2,592:
Or, as a functional expression (rather than an imperative procedure):
<
(function () {
'use strict';
Line 2,491 ⟶ 2,609:
})();
</syntaxhighlight>
{{Out}}
Line 2,499 ⟶ 2,617:
===ES6===
<
"use strict";
Line 2,526 ⟶ 2,644:
// MAIN ---
return main();
})();</
{{Out}}
<
=={{header|Joy}}==
For matrices represented as lists, there's ''transpose'', defined in seqlib like this:
<syntaxhighlight lang=
[pop []]
[[[first] map] [[rest] map] cleave]
Line 2,543 ⟶ 2,661:
The following definition of transpose/0 expects its input to be a non-empty array, each element of which should be an array of the same size. The result is an array that represents the transposition of the input.
<
if (.[0] | length) == 0 then []
else [map(.[0])] + (map(.[1:]) | transpose)
end ;</
'''Examples'''
[[], []] | transpose
Line 2,562 ⟶ 2,680:
First a module, shared by the Transposition, Multiplication and Exponentiation tasks.
<
function Matrix(ary) {
this.mtx = ary;
Line 2,620 ⟶ 2,738:
};
provide('Matrix', '0.60');</
Then a unitTest of the transposition.
<
require('Matrix');
Line 2,638 ⟶ 2,756:
m.transpose() ==> { height:4, mtx:[ [ 1, 2, 3, 4, 5 ], [ 1, 4, 9, 16, 25 ], [ 1, 8, 27, 64, 125 ], [ 1, 16, 81, 256, 625 ] ], width:5 }
=!EXPECTEND!=
*/</
{{out}}
Line 2,646 ⟶ 2,764:
=={{header|Julia}}==
The transposition is obtained by quoting the matrix.
<
2x3 Array{Int64,2}:
1 2 3
Line 2,655 ⟶ 2,773:
1 4
2 5
3 6</
If you do not want change the type, convert the result back to Array{Int64,2}.
Line 2,661 ⟶ 2,779:
=={{header|K}}==
Transpose is the monadic verb <code>+</code>
<
(1 1 1 1.0
2 4 8 16.0
Line 2,672 ⟶ 2,790:
1 4 9 16 25.0
1 8 27 64 125.0
1 16 81 256 625.0)</
=={{header|Klong}}==
Transpose is the monadic verb <code>+</code>
<
[[0 1 2 3 4]
[5 6 7 8 9]
Line 2,688 ⟶ 2,806:
[2 7 12 17 22]
[3 8 13 18 23]
[4 9 14 19 24]]</
=={{header|Kotlin}}==
<
typealias Vector = DoubleArray
Line 2,714 ⟶ 2,832:
}
}
}</
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{require lib_matrix}
{M.disp
{M.transp
[[1, 1, 1, 1],
[2, 4, 8, 16],
[3, 9, 27, 81],
[4, 16, 64, 256],
[5, 25,125, 625]]
}}
->
[[1,2,3,4,5],
[1,4,9,16,25],
[1,8,27,64,125],
[1,16,81,256,625]]
</syntaxhighlight>
=={{header|Lang5}}==
<
1 transpose .</
{{out}}
<pre>[
Line 2,733 ⟶ 2,873:
=={{header|LFE}}==
<
(defun transpose (matrix)
(transpose matrix '()))
Line 2,747 ⟶ 2,887:
(tails (lists:map #'cdr/1 matrix)))
(transpose tails (++ acc `(,heads)))))))
</syntaxhighlight>
Usage in the LFE REPL:
<
> (transpose '((1 2 3)
(4 5 6)
Line 2,760 ⟶ 2,900:
((1 4 7 10 13 16) (2 5 8 11 14 17) (3 6 9 12 15 18))
>
</syntaxhighlight>
=={{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.
<
print "Transpose of matrix"
Line 2,770 ⟶ 2,910:
print " ="
MatrixT$ =MatrixTranspose$( MatrixC$)
call DisplayMatrix MatrixT$</
{{out}}
Line 2,785 ⟶ 2,925:
=={{header|Lua}}==
<
local res = {}
Line 2,807 ⟶ 2,947:
end
io.write( "\n" )
end</
Using apply map list
<
local b = {}
for k,v in ipairs(a) do b[k] = f(v) end
Line 2,836 ⟶ 2,976:
yx = apply(mapn, function(...) return {...} end, xy)
print(table.concat(map(function(a) return table.concat(a,",") end, xy), "\n"),"\n")
print(table.concat(map(function(a) return table.concat(a,",") end, yx), "\n"))</
--Edit: table.getn() deprecated, using # instead
Line 2,844 ⟶ 2,984:
The <code>Transpose</code> function in Maple's <code>LinearAlgebra</code> package computes this. The computation can also be accomplished by raising the Matrix to the <code>%T</code> power. Similarly for <code>HermitianTranspose</code> and the <code>%H</code> power.
<syntaxhighlight lang="maple">
M := <<2,3>|<3,4>|<5,6>>;
Line 2,851 ⟶ 2,991:
with(LinearAlgebra):
Transpose(M);
</syntaxhighlight>
{{out}}
<pre>
Line 2,872 ⟶ 3,012:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{2, 4, 8, 16},
{3, 9, 27, 81},
{4, 16, 64, 256},
{5, 25, 125, 625}}
transposedMatrix = Transpose[originalMatrix]</
=={{header|MATLAB}}==
Matlab contains two built-in methods of transposing a matrix: by using the <code>transpose()</code> function, or by using the <code>.'</code> operator. The <code>'</code> operator yields the [[conjugate transpose|complex conjugate transpose]].
<
ans =
Line 2,893 ⟶ 3,033:
1 3
2 4</
But, you can, obviously, do the transposition of a matrix without a built-in method, in this case, the code can be this hereafter code:
<syntaxhighlight lang="matlab">
B=size(A); %In this code, we assume that a previous matrix "A" has already been inputted.
Line 2,905 ⟶ 3,045:
end
</syntaxhighlight>
Transposing nested cells using apply map list
<
yx = feval(@(x) cellfun(@(varargin)[varargin],x{:},'un',0), xy)</
=={{header|Maxima}}==
<
[2, 4, 8, 16],
[3, 9, 27, 81],
[4, 16, 64, 256],
[5, 25, 125, 625]);
transposedMatrix : transpose(originalMatrix);</
=={{header|MAXScript}}==
Uses the built in transpose() function
<
for i in 1 to 5 do for j in 1 to 4 do m[i][j] = pow i j
m = transpose m</
=={{header|Nial}}==
make an array
<
=1 2 3
=4 5 6</
transpose it
<
=1 4
=2 5
=3 6</
=={{header|Nim}}==
For statically sized arrays:
<
for i in low(X)..high(X):
for j in low(Y)..high(Y):
Line 2,950 ⟶ 3,090:
for i in r:
stdout.write i, " "
echo ""</
{{out}}
<pre> 0 5 1
Line 2,958 ⟶ 3,098:
4 9 42 </pre>
For dynamically sized seqs:
<
result = newSeq[seq[T]](s[0].len)
for i in 0 .. s[0].high:
Line 2,968 ⟶ 3,108:
@[ 5, 6, 7, 8, 9],
@[ 1, 0, 0, 0, 42]]
echo transpose(a)</
{{out}}
<pre>@[@[0, 5, 1], @[1, 6, 0], @[2, 7, 0], @[3, 8, 0], @[4, 9, 42]]</pre>
=={{header|Objeck}}==
<
bundle Default {
class Transpose {
Line 3,005 ⟶ 3,145:
}
}
</syntaxhighlight>
{{out}}
Line 3,019 ⟶ 3,159:
The implementation below uses a bigarray:
<
let transpose b =
Line 3,048 ⟶ 3,188:
[| 5; 6; 7; 8 |];
|]
;;
array2_display (Printf.printf " %d") print_newline (transpose a) ;;</
{{out}}
Line 3,059 ⟶ 3,200:
</pre>
A version for lists:
<
assert (m <> []);
if List.mem [] m then
[]
else
List.map List.hd m :: transpose (List.map List.tl m)</
Example:
# transpose [[1;2;3;4];
Line 3,071 ⟶ 3,212:
=={{header|Octave}}==
<
2, 4, 8, 16 ;
3, 9, 27, 81 ;
Line 3,077 ⟶ 3,218:
5, 25, 125, 625 ];
tranposed = a.'; % tranpose
ctransp = a'; % conjugate transpose</
=={{header|OxygenBasic}}==
<
function Transpose(double *A,*B, sys nx,ny)
'==========================================
Line 3,123 ⟶ 3,264:
Transpose A,B,5,4
print MatrixShow B,4,5
</syntaxhighlight>
=={{header|PARI/GP}}==
The GP function for matrix (or vector) transpose is <code>mattranspose</code>, but it is usually invoked with a tilde:
<syntaxhighlight lang
In PARI the function is
<syntaxhighlight lang
though <code>shallowtrans</code> is also available when deep copying is not desired.
=={{header|Pascal}}==
<
const
Line 3,165 ⟶ 3,306:
writeln;
end;
end.</
{{out}}
<pre>% ./Transpose
Line 3,178 ⟶ 3,319:
4 9 14
5 10 15
</pre>
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
uses NumLibABC;
begin
var A := new Matrix(4, 3,
1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12);
'Original:'.Println;
A.Println(3, 0);
'Transposed:'.Println;
A.Transpose.Println(3, 0);
end.
</syntaxhighlight>
{{out}}
<pre>
Original:
1 2 3
4 5 6
7 8 9
10 11 12
Transposed:
1 4 7 10
2 5 8 11
3 6 9 12
</pre>
=={{header|Perl}}==
{{libheader|Math::Matrix}}
<
$m = Math::Matrix->new(
Line 3,192 ⟶ 3,362:
);
$m->transpose->print;</
{{out}}
Line 3,202 ⟶ 3,372:
</pre>
Manually:
<
[1, 1, 1, 1],
[2, 4, 8, 16],
Line 3,213 ⟶ 3,383:
foreach my $j (0..$#{$m[0]}) {
push(@transposed, [map $_->[$j], @m]);
}</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">matrix_transpose</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">mat</span><span style="color: #0000FF;">)</span>
Line 3,229 ⟶ 3,399:
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<!--</
=={{header|PHP}}==
====Up to PHP version 5.6====
<
function transpose($m) {
if (count($m) == 0) // special case: empty matrix
Line 3,243 ⟶ 3,413:
array_unshift($m, NULL); // the original matrix is not modified because it was passed by value
return call_user_func_array('array_map', $m);
}</
====Starting with PHP 5.6====
<
function transpose($m) {
return count($m) == 0 ? $m : (count($m) == 1 ? array_chunk($m[0], 1) : array_map(null, ...$m));
}
</syntaxhighlight>
=={{header|Picat}}==
Picat has a built-in function <code>transpose/1</code> (in the <code>util</code> module).
<
go =>
Line 3,299 ⟶ 3,469:
end,
M := M ++ [NewRow]
end.</
{{out}}
Line 3,343 ⟶ 3,513:
=={{header|PicoLisp}}==
<
(apply mapcar Mat list) )
(matTrans '((1 2 3) (4 5 6)))</
{{out}}
<pre>-> ((1 4) (2 5) (3 6))</pre>
=={{header|PL/I}}==
<
declare A(m, n) float, B (n,m) float defined (A(2sub, 1sub));
/* Any reference to B gives the transpose of matrix A. */</
Traditional method:
<
transpose: procedure (a, b);
declare (a, b) (*,*) float controlled;
Line 3,369 ⟶ 3,539:
b(*,i) = a(i,*);
end;
end transpose;</
=={{header|Pop11}}==
<
lvars bl = boundslist(m);
if length(bl) /= 4 then
Line 3,385 ⟶ 3,555:
endfor;
endfor;
enddefine;</
=={{header|PostScript}}==
{{libheader|initlib}}
<
[ exch {
{ {empty? exch pop} map all?} {pop exit} ift
[ exch {} {uncons {exch cons} dip exch} fold counttomark 1 roll] uncons
} loop ] {reverse} map
}.</
=={{header|PowerBASIC}}==
PowerBASIC has the MAT statement to simplify Matrix Algebra calculations; in conjunction with the TRN operation the actual transposition is just a one-liner.
<
#DIM ALL
#COMPILER PBCC 6
Line 3,451 ⟶ 3,621:
TranspositionDemo 3, 3
TranspositionDemo 3, 7
END FUNCTION</
{{out}}
<pre>initial matrix:
Line 3,476 ⟶ 3,646:
=={{header|PowerShell}}==
===Any Matrix===
<syntaxhighlight lang="powershell">
function transpose($a) {
$arr = @()
Line 3,535 ⟶ 3,705:
"transpose `$a ="
show (transpose $a)
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 3,582 ⟶ 3,752:
===Square Matrix===
<syntaxhighlight lang="powershell">
function transpose($a) {
if($a) {
Line 3,605 ⟶ 3,775:
""
show (transpose $a)
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 3,621 ⟶ 3,791:
In Prolog, a matrix is a list of lists. transpose/2 can be written like that.
{{works with|SWI-Prolog}}
<
% e.g. [[1,2,3,4], [5,6,7,8]]
% give [[1,5],[2,6],[3,7],[4,8]]
Line 3,647 ⟶ 3,817:
% "quick" append
append_dl(X-Y, Y-Z, X-Z).</
=={{header|PureBasic}}==
Matrices represented by integer arrays using rows as the first dimension and columns as the second dimension.
<
Protected rows, cols
Line 3,702 ⟶ 3,872:
Input()
CloseConsole()
EndIf</
{{out}}
<pre>matrix m, before: (3, 4)
Line 3,716 ⟶ 3,886:
=={{header|Python}}==
<
(2, 4, 8, 16),
(3, 9, 27, 81),
Line 3,723 ⟶ 3,893:
print(zip(*m))
# in Python 3.x, you would do:
# print(list(zip(*m)))</
{{out}}
<pre>
Line 3,741 ⟶ 3,911:
Perhaps, for example, something like:
<
def transpose(m):
if m:
Line 3,781 ⟶ 3,951:
(' of ' + type(tm[0]).__name__) if m else ''
) + ' :: ' + str(m) + ' -> ' + str(tm)
)</
{{Out}}
<pre>transpose function :: (Transposition without type change):
Line 3,797 ⟶ 3,967:
If any of the rows in a '''list of lists''' matrix are not wide enough for a full set of data for one or more of the columns, then '''zip(*xs)''' will drop all the data entirely, without warning or error message, returning no more than an empty list:
<
uls = [[10, 11], [20], [], [30, 31, 32]]
Line 3,804 ⟶ 3,974:
)
# --> []</
At this point, short of turning to '''numpy''', we might need to write a custom function.
Line 3,811 ⟶ 3,981:
{{Works with|Python|3.7}}
<
from collections import defaultdict
Line 3,940 ⟶ 4,110:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Transposition of row sets with possible gaps:
Line 3,954 ⟶ 4,124:
=={{header|Quackery}}==
<
over 0 peek size of
[] rot
Line 3,966 ⟶ 4,136:
dup echo cr cr
transpose dup echo cr cr
transpose echo cr</
{{out}}
Line 3,977 ⟶ 4,147:
=={{header|R}}==
<
m <- matrix(c(b, b^2, b^3, b^4), 5, 4)
print(m)
tm <- t(m)
print(tm)</
=={{header|Racket}}==
<
#lang racket
(require math)
(matrix-transpose (matrix [[1 2] [3 4]]))
</syntaxhighlight>
{{out}}
<pre>
Line 3,999 ⟶ 4,169:
(formerly Perl 6)
{{Works with|rakudo|2018.03}}
<syntaxhighlight lang="raku"
# on list-of-lists data structures
Line 4,020 ⟶ 4,190:
}
say @b;</
{{output}}
Line 4,028 ⟶ 4,198:
=={{header|Rascal}}==
<
return {<y, x, v> | <x, y, v> <- matrix};
}
Line 4,037 ⟶ 4,207:
<1.0,0.0,-51.0>, <1.0,1.0,167.0>, <1.0,2.0,24.0>,
<2.0,0.0,4.0>, <2.0,1.0,-68.0>, <2.0,2.0,-41.0>
};</
=={{header|REXX}}==
<
@.=; @.1 = 1.02 2.03 3.04 4.05 5.06 6.07 7.08
@.2 = 111 2222 33333 444444 5555555 66666666 777777777
Line 4,064 ⟶ 4,234:
end /*c*/
say _ /*1 line.*/
end /*r*/; return</
{{out|output|text= when using the default input:}}
<pre>
Line 4,082 ⟶ 4,252:
=={{header|Ring}}==
<
load "stdlib.ring"
transpose = newlist(5,4)
Line 4,093 ⟶ 4,263:
see nl
next
</syntaxhighlight>
Output:
<pre>
Line 4,104 ⟶ 4,274:
=={{header|RLaB}}==
<
0.41844289 0.476591435 0.75054022 0.226388925 0.963880314
0.91267171 0.941762397 0.464227895 0.693482786 0.203839405
Line 4,113 ⟶ 4,283:
0.75054022 0.464227895 0.26582235
0.226388925 0.693482786 0.11557427
0.963880314 0.203839405 0.0442493069</
=={{header|RPL}}==
[[1 2 3 4][5 6 7 8][9 10 11 12]] TRN
{{out}}
<pre>
[[1 5 9] [2 6 10] [3 7 11] [4 8 12]]
</pre>
=={{header|Ruby}}==
<
[2, 4, 8, 16],
[3, 9, 27, 81],
[4, 16, 64, 256],
[5, 25,125, 625]]
puts m.transpose</
{{out}}
<pre>
Line 4,127 ⟶ 4,304:
</pre>
or using 'matrix' from the standard library
<
m=Matrix[[1, 1, 1, 1],
Line 4,134 ⟶ 4,311:
[4, 16, 64, 256],
[5, 25,125, 625]]
puts m.transpose</
{{out}}
<pre>
Line 4,140 ⟶ 4,317:
</pre>
or using zip:
<
m[0].zip(*m[1..-1])
end
p transpose([[1,2,3],[4,5,6]])</
{{out}}
<pre>
Line 4,150 ⟶ 4,327:
=={{header|Run BASIC}}==
<
print "Transpose of matrix"
Line 4,185 ⟶ 4,362:
next i
html "</table>"
end sub</
{{out}}
Transpose of matrix<br><table border=2><tr align=right><td>0</td><td>0.1</td><td>0.2</td><td>0.3</td></tr><tr align=right><td>0.4</td><td>0.5</td><td>0.6</td><td>0.7</td></tr><tr align=right><td>0.8</td><td>0.9</td><td>1.0</td><td>1.1</td></tr></table> =<br />
Line 4,192 ⟶ 4,369:
=={{header|Rust}}==
===version 1===
<
struct Matrix {
dat: [[i32; 3]; 3]
Line 4,241 ⟶ 4,418:
c.print();
}
</syntaxhighlight>
===version 2===
<
fn main() {
let m = vec![vec![1, 2, 3], vec![4, 5, 6]];
Line 4,270 ⟶ 4,447:
t
}
</syntaxhighlight>
<b>Output:</b>
Line 4,285 ⟶ 4,462:
=={{header|Scala}}==
<
res12: Array[Array[Int]] = Array(Array(0, 1, 2, 3), Array(4, 5, 6, 7), Array(8, 9, 10, 11), Array(12, 13, 14, 15))
Line 4,303 ⟶ 4,480:
1 5 9 13
2 6 10 14
3 7 11 15</
=={{header|Scheme}}==
<
(apply map list m))</
=={{header|Seed7}}==
<
include "float.s7i";
Line 4,355 ⟶ 4,532:
writeln("After Transposition:");
write(transpose(testMatrix));
end func;</
{{out}}
Line 4,371 ⟶ 4,548:
</pre>
=={{header|SETL}}==
<syntaxhighlight lang="setl">program matrix_transposition;
mat := [[1,2,3,4], [5,6,7,8], [9,10,11,12]];
print(mat);
print(' -> ');
print(transpose(mat));
proc transpose(m);
return [[m(y)(x) : y in [1..#m]] : x in [1..#m(1)]];
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>[[1 2 3 4] [5 6 7 8] [9 10 11 12]]
->
[[1 5 9] [2 6 10] [3 7 11] [4 8 12]]</pre>
=={{header|Sidef}}==
<
matrix[0].range.map{|i| matrix.map{_[i]}};
};
Line 4,386 ⟶ 4,578:
transpose(m).each { |row|
"%5d" * row.len -> printlnf(row...);
}</
{{out}}
<pre> 1 2 3 4 5
Line 4,397 ⟶ 4,589:
{{works with|OpenAxiom}}
{{works with|Axiom}}
<
[3, 9, 27, 81],[4, 16, 64, 256], _
[5, 25, 125, 625]]
Line 4,420 ⟶ 4,612:
| |
+1 16 81 256 625+
Type: Matrix(Integer)</
Domain:[http://fricas.github.io/api/Matrix.html?highlight=matrix Matrix(R)]
=={{header|Sparkling}}==
<
return map(range(sizeof A), function(k, idx) {
return map(A, function(k, row) {
Line 4,431 ⟶ 4,623:
});
});
}</
=={{header|Stata}}==
Line 4,437 ⟶ 4,629:
=== Stata matrices ===
<
. mat b=a'
. mat list a
Line 4,452 ⟶ 4,644:
c1 1 4
c2 2 5
c3 3 6</
=== Mata ===
<
: a
Line 4,475 ⟶ 4,667:
1 | 1 |
2 | 1i |
+------+</
=={{header|Swift}}==
<
public func matrixTranspose<T>(_ matrix: [[T]]) -> [[T]] {
guard !matrix.isEmpty else {
Line 4,528 ⟶ 4,720:
print("Output:")
printMatrix(m2)</
{{out}}
Line 4,541 ⟶ 4,733:
=={{header|Tailspin}}==
<
templates transpose
def a: $;
Line 4,568 ⟶ 4,760:
' -> !OUT::write
$mT -> printMatrix&{w:2} -> !OUT::write
</syntaxhighlight>
{{out}}
<pre>
Line 4,585 ⟶ 4,777:
=={{header|Tcl}}==
With core Tcl, representing a matrix as a list of lists:
<
namespace path ::tcl::mathfunc
Line 4,628 ⟶ 4,820:
set m {{1 1 1 1} {2 4 8 16} {3 9 27 81} {4 16 64 256} {5 25 125 625}}
print_matrix $m "%d"
print_matrix [transpose $m] "%d"</
{{out}}
<pre>1 1 1 1
Line 4,640 ⟶ 4,832:
1 16 81 256 625</pre>
{{tcllib|struct::matrix}}
<
struct::matrix M
M deserialize {5 4 {{1 1 1 1} {2 4 8 16} {3 9 27 81} {4 16 64 256} {5 25 125 625}}}
M format 2string
M transpose
M format 2string</
{{out}}
<pre>1 1 1 1
Line 4,673 ⟶ 4,865:
=={{header|True BASIC}}==
<
DIM matriz(3, 4)
DATA 78, 19, 30, 12, 36
Line 4,703 ⟶ 4,895:
PRINT
NEXT fila
END</
=={{header|Ursala}}==
Matrices are stored as lists of lists, and transposing them is a built in operation.
<
example =
Line 4,715 ⟶ 4,907:
<1.,2.,3.,4.>,
<5.,6.,7.,8.>,
<9.,10.,11.,12.>></
For a more verbose version, replace the ~&K7 operator with the standard library function
named transpose.
Line 4,728 ⟶ 4,920:
=={{header|VBA}}==
<
transpose = WorksheetFunction.transpose(m)
End Function</
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
'create and display the initial matrix
WScript.StdOut.WriteLine "Initial Matrix:"
Line 4,765 ⟶ 4,957:
WScript.StdOut.WriteLine
Next
</syntaxhighlight>
{{Out}}
Line 4,790 ⟶ 4,982:
{{works with|Visual Basic|5}}
{{works with|Visual Basic|6}}
<
'----------------------------------------------------------------------
Function TransposeMatrix(InitMatrix() As Long, TransposedMatrix() As Long)
Line 4,845 ⟶ 5,037:
TranspositionDemo 3, 3
TranspositionDemo 3, 7
End Sub</
{{out}}
<pre>initial matrix:
Line 4,870 ⟶ 5,062:
=={{header|Wortel}}==
The <code>@zipm</code> operator zips together an array of arrays, this is the same as transposition if the matrix is represented as an array of arrays.
<
Returns:
<pre>[[1 4 7] [2 5 8] [3 6 9]]</pre>
Line 4,877 ⟶ 5,069:
{{libheader|Wren-matrix}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var m = Matrix.new([
Line 4,890 ⟶ 5,082:
Fmt.mprint(m, 2, 0)
System.print("\nTransposed:\n")
Fmt.mprint(m.transpose, 2, 0)</
{{out}}
Line 4,912 ⟶ 5,104:
unusual array configuration. It can't simply reorder the elements stored
in the original array's memory.
<
int M, R, C, N; \rows and columns
int I, J;
Line 4,939 ⟶ 5,131:
Transpose(M, 3, 4, N);
ShowMat(N, 4, 3);
]</
{{out}}
Line 4,955 ⟶ 5,147:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<
dim mtranspuesta(5,4)
Line 4,979 ⟶ 5,171:
data 49,10,65,42,50
data 30,93,24,78,10
data 39,68,27,64,29</
=={{header|zkl}}==
Using the GNU Scientific Library:
<
GSL.Matrix(2,3).set(1,2,3, 4,5,6).transpose().format(5).println(); // in place
println("---");
GSL.Matrix(2,2).set(1,2, 3,4).transpose().format(5).println(); // in place
println("---");
GSL.Matrix(3,1).set(1,2,3).transpose().format(5).println(); // in place</
{{out}}
<pre>
Line 5,003 ⟶ 5,195:
Or, using lists:
{{trans|Wortel}}
<
if(M.len()==1) M[0].pump(List,List.create); // 1 row --> n columns
else M[0].zip(M.xplode(1));
}</
The list xplode method pushes list contents on to the call stack.
<
m:=L(L(1,"a"),L(2,"b"),L(3,"c")); transpose(m).println();
transpose(L(L(1,2,3))).println();
transpose(L(L(1),L(2),L(3))).println();
transpose(L(L(1))).println();</
{{out}}
<pre>
Line 5,023 ⟶ 5,215:
=={{header|zonnon}}==
<
module MatrixOps;
type
Line 5,053 ⟶ 5,245:
Transposition;
end MatrixOps.
</syntaxhighlight>
|