mrdivide — Solve X * B = A using MATLAB's right-division operator (/).
X = A / B (or mrdivide(A, B)) solves the right-sided linear system X * B = A. When B is square and nonsingular the solution matches A * inv(B). Rectangular or rank-deficient matrices are handled via a minimum-norm least-squares solve, matching MATLAB's SVD-based semantics.
How mrdivide works in RunMat
- 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.
How mrdivide runs 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.2857iFAQ
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 functions to explore
These functions work well alongside mrdivide. Each page has runnable examples you can try in the browser.
mtimes, svd, lu, gpuArray, gather, ctranspose, dot, mldivide, mpower, trace, transpose
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how mrdivide works, line by line, in Rust.
- View mrdivide.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.