__slash__
- Documentation: slash ¶
The slash and backslash operators cover the different flavors of division.
\Matrix Left Division (“backslash”)Solves the linear system Ax = b for x.
A \ bplays the same role asinv (A) * b, but is computed directly and more accurately, without ever forming the inverse of A.Example:
A = [1 2; 3 4]; b = [4; 2]; x = A \ b ⇒ x = -6 5 isequal (A*x, b) ⇒ 1When the system is non-square, or A is singular, the operator instead returns the minimum-norm solution (the x minimizing
norm (A*x - b)).Dense matrices are solved via Gaussian elimination with partial pivoting. Sparse matrices go through a direct LU factorization instead (see ‘
lu’); this keeps “fill-in” of new nonzeros low, though it can still be memory-hungry. When that is a concern, an iterative solver such as see ‘cgs’ or see ‘gmres’ may be a better fit./Matrix Right DivisionThe forward-slash form solves AB = C for A via
A = C / B../and.\Component-wise “Hadamard” DivisionThese “dot slash” operators divide element-by-element, e.g.:
A = [1 10 12; 24 30 42]; B = [1 5 4; 6 6 7]; A ./ B ⇒ 1 2 3 4 5 6Operands of differing size may still combine via broadcasting (see ‘Broadcasting’):
420 ./ B ⇒ 420 84 105 70 70 60 [20; 42] ./ B ⇒ 20 4 5 7 7 6