mrdivide — Solve linear systems with right-division in MATLAB and RunMat.
X = A / B (or mrdivide(A, B)) solves X * B = A. Square/nonsingular, rectangular, and rank-deficient solve paths follow MATLAB right-division semantics.
Syntax
X = mrdivide(A, B)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Left-hand side matrix or scalar. |
B | Any | Yes | — | Right coefficient matrix or scalar. |
Returns
| Name | Type | Description |
|---|---|---|
X | NumericArray | Solution to X * B = A. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:mrdivide:InvalidInput | Inputs are unsupported or incompatible for right division. | mrdivide: invalid input |
RunMat:mrdivide:Internal | Runtime cannot materialize right-division outputs. | mrdivide: internal runtime failure |
How mrdivide works
- Scalars divide exactly:
A / sscalesAby1/s, whiles / BrequiresBto be scalar. - Logical and integer inputs are promoted to double precision before solving.
- Purely real operands produce real outputs; any complex operand promotes the computation (and result) to complex arithmetic.
- Inputs must be effectively two-dimensional; trailing singleton dimensions are allowed.
- The number of columns must agree (
size(A, 2) == size(B, 2)), otherwise RunMat raises the MATLAB error"Matrix dimensions must agree." - Underdetermined and overdetermined systems return the minimum-norm least-squares solution.
Does RunMat run mrdivide on the GPU?
When a gpuArray provider is active, RunMat first offers the solve to its mrdivide hook. The WGPU provider currently downloads the operands to the host, executes the same SVD-based solver used by the CPU implementation, then uploads the result back to the device so residency remains transparent. If no provider is available—or the provider declines the request—RunMat gathers any gpuArray inputs to the host, computes the solution, and returns a host tensor.
GPU memory and residency
No manual care is required. If both operands already reside on the GPU and the provider supports mrdivide, the solve stays on the device. When the provider falls back to the host (the current WGPU implementation), the runtime seamlessly gathers data, executes the solve, and re-uploads the result to keep downstream GPU pipelines working as expected.
Examples
Solving a square linear system
A = [1 2; 3 4];
B = [5 6; 7 8];
X = A / B;
% Verify the solution
residual = X * BExpected output:
X =
3 -2
2 -1
residual =
1 2
3 4Computing a least-squares right division
A = [1 2 3];
B = [1 0 1; 0 1 1];
X = A / BExpected output:
X = [1 2]Dividing by a scalar
A = [2 4 6];
scaled = A / 2Expected output:
scaled = [1 2 3]Right division with complex inputs
A = [1+2i 3-4i];
B = [2-i 1+i];
X = A / BExpected output:
X = -0.1429 - 0.2857iUsing mrdivide with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how mrdivide changes the result.
Run a small mrdivide example, explain the result, then change one input and compare the output.
FAQ
Why must A and B share the number of columns?⌄
Right division solves X * B = A; matrix multiplication requires size(A, 2) == size(B, 2).
What happens if B is singular or rectangular?⌄
RunMat matches MATLAB by computing the minimum-norm least-squares solution via singular-value decomposition—no explicit call to pinv is required.
Does mrdivide support higher-dimensional arrays?⌄
No. Inputs must be effectively matrices (trailing singleton dimensions are allowed). Use reshape or (:) to flatten higher-dimensional data before calling mrdivide.
How are logical or integer arrays handled?⌄
They are promoted to double precision (true → 1, false → 0) before solving, matching MATLAB semantics.
How does RunMat handle NaN or Inf values?⌄
They propagate through the least-squares solve in the same way as MATLAB. NaNs in the inputs yield NaNs in the output wherever they influence the solution.
Related Linalg functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how mrdivide is executed, line by line, in Rust.
- View the source for mrdivide 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.