View all functions

CategoryMath: Linalg & Ops
BLAS/LAPACK

What does the transpose function do in MATLAB / RunMat?

B = transpose(A) flips the first two dimensions of A without conjugating complex values. It is equivalent to the MATLAB syntax A.' and leaves higher dimensions untouched.

How does the transpose function behave in MATLAB / RunMat?

  • Works for scalars, vectors, matrices, and N-D arrays; only the first two axes are swapped.
  • Complex values are not conjugated. Use ctranspose/A' for conjugate transpose.
  • Logical, string, character, and cell arrays preserve their types and metadata.
  • Vectors become column or row matrices as needed (e.g., size(transpose(1:3)) == [3 1]).
  • Empty and singleton dimensions follow MATLAB's column-major semantics.

transpose Function GPU Execution Behaviour

When a gpuArray is provided, RunMat first asks the active Accel provider for a dedicated transpose kernel. The WGPU backend ships such a kernel today; other providers may supply their own implementation. If the hook is missing, RunMat gathers the data to the host, performs the transpose once, and re-uploads it so downstream GPU work continues without residency churn.

Examples of using the transpose function in MATLAB / RunMat

Transposing a numeric matrix to swap rows and columns

A = [1 2 3; 4 5 6];
B = transpose(A);

Expected output:

B =
     1     4
     2     5
     3     6

Turning a row vector into a column vector

row = 1:4;
col = transpose(row);
size(col)

Expected output:

ans = [4 1]

Preserving imaginary parts when transposing complex matrices

Z = [1+2i 3-4i];
ZT = transpose(Z);

Expected output:

ZT =
     1.0000 + 2.0000i
     3.0000 - 4.0000i

Transposing logical masks while keeping logical type

mask = logical([1 0 1; 0 1 0]);
maskT = transpose(mask);
class(maskT)

Expected output:

ans = 'logical'

Transposing character arrays to flip rows and columns of text

C = ['r' 'u' 'n'; 'm' 'a' 't'];
CT = transpose(C);

Expected output:

CT =
    'rm'
    'ua'
    'nt'

Transposing gpuArray data without leaving the device

G = gpuArray(rand(1024, 32));
GT = transpose(G);
isgpuarray(GT)

Expected output:

ans = logical 1

GPU residency in RunMat (Do I need gpuArray?)

No additional residency management is required. If the planner keeps your data on the GPU, transpose will honour that residency and either invoke a provider kernel or, in the worst case, perform a gather/transpose/upload round-trip automatically.

FAQ

  1. Does transpose conjugate complex numbers?
    No. Use ctranspose or the ' operator for conjugate transpose.
  2. What happens for tensors with more than two dimensions?
    Only the first two axes are swapped; higher dimensions remain in-place.
  3. Do empty matrices stay empty after transposition?
    Yes. MATLAB's empty-dimension rules are preserved exactly.
  4. Is the result a copy or a view?
    It is a new array. Neither the input nor the output share storage.
  5. Can I transpose cell arrays?
    Yes—RunMat mirrors MATLAB by rearranging each cell handle into the new layout.
  6. Are logical arrays still logical after transpose?
    Absolutely. The data stay in compact logical storage.
  7. How does transpose interact with the fusion planner?
    Fusion treats transposes as pipeline boundaries, so kernels before and after the transpose can still fuse independently.
  8. What if my provider lacks a transpose kernel?
    RunMat transparently gathers, transposes on the host, and re-uploads while logging a warning.
  9. Does transpose change sparse matrices?
    Sparse support is planned; current releases operate on dense arrays.
  10. Can I compose transpose with permute?
    Yes—transpose is equivalent to permute(A, [2 1 3 ...]).

See Also

ctranspose, permute, mtimes, gpuArray, gather

Source & Feedback