Logo

Programming-Idioms

Declare two two-dimensional arrays a and b of dimension n*m and m*n, respectively. Assign to b the transpose of a (i.e. the value with index interchange).
Implementation
Clojure

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Clojure implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
real :: a(n,m), b(m,n)

b = transpose(a)
a = [[1,2], [3,4], [5,6]]
b = a.transpose
uses typ, omv;
var
  A: array[1..m, 1..n] of ArbFloat;
  B: array[1..n, 1..m] of ArbFloat;
begin
  ... some code to fill A
  omvtrm(
    A[1,1], m, n, n,
    B[1,1], m
  );
end.
use PDL::Basic qw(sequence transpose);
my ($m, $n) = (3, 2);
my $A = sequence $m, $n;
my $B = transpose $A;
import numpy as np
a = np.array([[1,2], [3,4], [5,6]])
b = a.T
const a = [[1, 2, 3], [4, 5, 6]]
const m = a[0].length
const b = Array.from({ length: m }, (_, n) => a.map(row => row[n]))
a = [[1,2], [3,4], [5,6]]
b = list(map(list, zip(*a)))
local a = {}

for x = 1, n do
  local t = {}
  for y = 1, m do
    t[y] = {x, y}
  end
  a[x] = t
end
  
local b = {}

for y = 1, m do
  local t = {}
  for x = 1, n do
    t[x] = a[x][y]
  end
  b[y] = t
end
use nalgebra::DMatrix;
use rand::prelude::*;
let a = DMatrix::<u8>::from_fn(n, m, |_, _| rand::thread_rng().gen());
let b = a.transpose();