Array Operations
- MATLAB has two flavours of Matlab Arithmetic Operations: Matlab Matrix Operations and array operations.
- Array operations are the dot operations — element-wise — as opposed to the matrix operations of Matlab Matrix Operations.
- Mathematical functions in MATLAB default to array operations unless they are explicitly matrix operations: e.g.
sin,sqrt,exp, andlogare element-wise. The corresponding Matlab Matrix Operations are usually distinguished by anmsuffix, e.g.sqrtm,expm,logm,mtimes,mpower.
Operators
The basic unary and binary array operations all have an operator form:
| Operator | Equivalent Function |
|---|---|
+ | plus |
- | minus |
.* | times |
./ | rdivide |
.\ | ldivide |
.^ | power |
Notes
-
All of the array operations above are binary; their operands must satisfy Matlab Compatible Array Sizes. The operation is performed by first expanding both operands to a common size, then applying the operation element-by-element on equal indices
-
For example:
>> a = [1 2 3]; b = [1 2 3]'; >> a .* b % equivalent to repmat(a,3,1) .* repmat(b,1,3) ans = 1 2 3 2 4 6 3 6 9
-
-
Addition and subtraction coincide for array and matrix operations, so there is no separate dotted form
-
A ./ Bdivides element-wise with A as the numerator and B as the denominator -
A .\ Bdivides element-wise with A as the denominator and B as the numerator