ctranspose — Conjugate-transpose arrays in MATLAB and RunMat.
B = ctranspose(A) (or A') swaps the first two dimensions of A and conjugates complex values. Real-valued inputs therefore behave like simple transpose, with MATLAB/RunMat Hermitian semantics for complex data.
Syntax
B = ctranspose(A)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input scalar/array value. |
Returns
| Name | Type | Description |
|---|---|---|
B | Any | Input value with first two dimensions swapped and complex conjugated. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:ctranspose:InvalidArgument | Call does not provide exactly one input argument. | ctranspose: invalid argument |
RunMat:ctranspose:InvalidInput | Input type is unsupported for conjugate transpose. | ctranspose: unsupported input type |
RunMat:ctranspose:Internal | Runtime cannot materialize conjugate-transpose output. | ctranspose: internal runtime failure |
How ctranspose works
- Works for scalars, vectors, matrices, and N-D arrays; only the first two axes are swapped.
- Real numeric, logical, and character data are not changed by conjugation.
- Complex values receive element-wise conjugation after the transpose (
(a + bi)' = a - bi). - Character arrays and cell arrays preserve their types;
ctransposesimply rearranges entries. - String scalars are passed through unchanged; string arrays transpose like MATLAB.
- Empty arrays and singleton dimensions follow MATLAB's column-major semantics.
Does RunMat run ctranspose on the GPU?
Provider support: If the backend exposes both transpose (or permute) and unary_conj, real and complex-interleaved tensors complete on the device without a gather.
Partial hooks: If the transpose succeeds but conjugation fails, RunMat falls back to the host path while logging a warning so users know their backend is incomplete.
No hooks: RunMat gathers the tensor, applies the conjugate transpose on the CPU, and re-uploads the result when possible so downstream kernels can keep running on the GPU. Complex fallback preserves interleaved GPU storage metadata when the result remains complex.
GPU memory and residency
No additional residency management is required. If the planner keeps real or complex data on the GPU, ctranspose honours that residency and either executes on the device (when hooks are present) or performs a storage-preserving gather/transpose/upload round-trip automatically.
Examples
Conjugate transpose of a complex matrix
Z = [1+2i 3-4i; 5+0i 6-7i];
H = ctranspose(Z)Expected output:
H =
1 - 2i 5 - 0i
3 + 4i 6 + 7iConjugate transpose of a real matrix equals the plain transpose
A = [1 2 3; 4 5 6];
B = ctranspose(A)Expected output:
B =
1 4
2 5
3 6Conjugate transpose turns row vectors into column vectors
row = [1-2i, 3+4i, 5];
col = ctranspose(row);
size(col)Expected output:
ans = [3 1]Conjugate transpose of a complex scalar
z = 2 + 3i;
result = ctranspose(z)Expected output:
result = 2 - 3iConjugate transpose of text data preserves characters
C = ['r' 'u' 'n'; 'm' 'a' 't'];
CT = ctranspose(C)Expected output:
CT =
'rm'
'ua'
'nt'Conjugate transpose of a gpuArray without leaving the device
G = gpuArray(rand(1024, 64) + 1i * rand(1024, 64));
GT = ctranspose(G)Using ctranspose with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how ctranspose changes the result.
Run a small ctranspose example, explain the result, then change one input and compare the output.
FAQ
How is ctranspose different from transpose?⌄
transpose (A.') swaps dimensions without conjugation; ctranspose (A') also conjugates complex values. For purely real data they are identical.
Does ctranspose change logical or character arrays?⌄
Only their layout changes. Values remain logical or character, and conjugation has no effect.
What about higher-dimensional arrays?⌄
Only the first two axes are swapped; trailing dimensions stay in-place, matching MATLAB.
Does the result share storage with the input?⌄
No. ctranspose materialises a fresh array, although fusion may eliminate the copy in optimised pipelines.
How are complex tensors handled on the GPU today?⌄
Complex gpuArray values use interleaved real/imaginary storage. Providers with transpose/permute and unary_conj hooks keep the conjugate transpose resident; fallback gathers and re-uploads through the same complex storage metadata.
Will ctranspose fuse with neighbouring kernels?⌄
Conjugate transposes currently act as fusion boundaries so that shape changes are visible to downstream kernels.
Can I rely on ctranspose inside linear-algebra routines (e.g., Hermitian products)?⌄
Yes. The builtin mirrors MATLAB semantics precisely and is safe to use inside idioms like A' * A.
What error do I get for unsupported types?⌄
Non-numeric objects (e.g., structs) raise ctranspose: unsupported input type ..., matching MATLAB's strict type checks.
Related Linalg functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how ctranspose is executed, line by line, in Rust.
- View the source for ctranspose in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.