If A is an empty 0-by-0 matrix, prod(A) returns 1. If A is a nonempty matrix, then prod(A) treats the columns of A as vectors and returns a row vector of the products of each column. If A is a vector, then prod(A) returns the product of the elements. Product of array elements returns the product of the array elements of A. If order n equals or exceeds the length of dimension dim, diff returns an empty array. It is the nth difference function calculated along the dimension specified by scalar dim. If X is a matrix, then diff(X) returns a matrix of row differences: Īpplies diff recursively n times, resulting in the nth difference. If X is a vector, then diff(X) returns a vector, one element shorter than X, of differences between adjacent elements: Returns the cumulative sum of the elements along dimension dim.ĭifferences and approximate derivatives calculates differences between adjacent elements of X. If A is a multidimensional array, then cumsum(A) acts along the first nonsingleton dimension. If A is a matrix, then cumsum(A) returns a matrix containing the cumulative sums for each column of A. If A is a vector, then cumsum(A) returns a vector containing the cumulative sum of the elements of A.
Returns the cumulative product along dimension dim.Ĭumulative sum returns an array A containing the cumulative sum. If A is a multidimensional array, then cumprod(A) acts along the first non-singleton dimension. If A is a matrix, then cumprod(A) returns a matrix containing the cumulative products for each column of A. If A is a vector, then cumprod(A) returns a vector containing the cumulative product of the elements of A. Solve systems of linear equations Ax = B for xĬumulative product returns an array of the same size as the array A containing the cumulative product. Solve systems of linear equations xA = B for x When you run the file, it produces the following result −Īpart from the above-mentioned arithmetic operators, MATLAB provides the following commands/functions used for similar purpose − Sr.No.
Matlab matrix code#
Create a script file with the following code − The following examples show the use of arithmetic operators on scalar data.
For complex matrices, this does not involve conjugation. For complex matrices, this is the complex conjugate transpose.Īrray transpose. A' is the linear algebraic transpose of A. A and B must have the same size, unless one of them is a scalar. A.^B is the matrix with elements A(i,j) to the B(i,j) power. For other values of p, the calculation involves eigenvalues and eigenvectors, such that if = eig(X), then X^p = V*D.^p/V.Īrray power. If the integer is negative, X is inverted first.
If p is an integer, the power is computed by repeated squaring. X^p is X to the power p, if p is a scalar. A.\B is the matrix with elements B(i,j)/A(i,j). A warning message is displayed if A is badly scaled or nearly singular.Īrray left division. If A is an n-by-n matrix and B is a column vector with n components, or a matrix with several such columns, then X = A\B is the solution to the equation AX = B. If A is a square matrix, A\B is roughly the same as inv(A)*B, except it is computed in a different way. A and B must have the same size, unless one of them is a scalar.īackslash or matrix left division. A./B is the matrix with elements A(i,j)/B(i,j). More precisely, B/A = (A'\B')'.Īrray right division. A.*B is the element-by-element product of the arrays A and B. A scalar can multiply a matrix of any size.Īrray multiplication. More precisely,įor non-scalar A and B, the number of columns of A must be equal to the number of rows of B. C = A*B is the linear algebraic product of the matrices A and B. A scalar can be subtracted from a matrix of any size. A and B must have the same size, unless one is a scalar. A scalar can be added to a matrix of any size. A+B adds the values stored in variables A and B. The following table gives brief description of the operators − Sr.No.Īddition or unary plus. However, as the addition and subtraction operation is same for matrices and arrays, the operator is same for both cases. The matrix operators and arrays operators are differentiated by the period (.) symbol. Array operations are executed element by element, both on one dimensional and multi-dimensional array. Matrix arithmetic operations are same as defined in linear algebra. MATLAB allows two different types of arithmetic operations −