Assume you are working on a new edition of MATLAB for The MathWorks. So far, in
ID: 3696550 • Letter: A
Question
Assume you are working on a new edition of MATLAB for The MathWorks. So far, in this new addition, MATLAB knows how to do the following:
• arithmetic operations (+, -, *,/)
• logical operations (<,<=,==,>=, >, &&, ||, ~,~=)
• conditional statements (if… elseif… else… end)
• loops (while loop, for loop)
• I/O (display, input())
• Scalars, vectors, and matrices
Create additional functionality in MATLAB by implementing the following functions:
• my_sqrt(x) calculates the square root of x
• my_abs(x) calculates the absolute value of x
• my_max(X) finds the maximum value of X and its position, i.e. index
• my_sort(X) sorts the values of X in increasing order
You can assume that x is a scalar and X is a vector. You can further assume that that x and X contain numerical values. You are NOT allowed to use any of MATLAB’s existing functions but need to design your own which should only use MATLAB’s capabilities described above.
Explanation / Answer
If A is a vector, then cumprod(A) returns a vector containing the cumulative product of the elements of A.
If A is a matrix, then cumprod(A) returns a matrix containing the cumulative products for each column of A.
If A is a multidimensional array, then cumprod(A) acts along the first non-singleton dimension.
If A is a vector, then cumsum(A) returns a vector containing the cumulative sum of the elements of A.
If A is a matrix, then cumsum(A) returns a matrix containing the cumulative sums for each column of A.
If A is a multidimensional array, then cumsum(A) acts along the first nonsingleton dimension.
If X is a vector, then diff(X) returns a vector, one element shorter than X, of differences between adjacent elements: [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)]
If X is a matrix, then diff(X) returns a matrix of row differences: [X(2:m,:)-X(1:m-1,:)]
If A is a vector, then prod(A) returns the product of the elements.
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 an empty 0-by-0 matrix, prod(A) returns 1.
If A is a multidimensional array, then prod(A) acts along the first non-singleton dimension and returns an array of products. The size of this dimension reduces to 1 while the sizes of all other dimensions remain the same.
The prod function computes and returns B as single if the input, A, is single. For all other numeric and logical data types, prod computes and returns B as double.
Sum of array elements; returns sums along different dimensions of an array. If A is floating point, that is double or single, B is accumulated natively, that is in the same class as A, and B has the same class as A. If A is not floating point, B is accumulated in double and B has class double.
If A is a vector, sum(A) returns the sum of the elements.
If A is a matrix, sum(A) treats the columns of A as vectors, returning a row vector of the sums of each column.
If A is a multidimensional array, sum(A) treats the values along the first non-singleton dimension as vectors, returning an array of row vectors.
sum(..., 'double')
sum(..., dim,'double')
sum(..., 'native')
sum(..., dim,'native')
idivide(a, b)
idivide(a, b,'fix')
Modulus after division; returns X - n.*Y where n = floor(X./Y). If Y is not an integer and the quotient X./Y is within round off error of an integer, then n is that integer. The inputs X and Y must be real arrays of the same size, or real scalars (provided Y ~=0).
Please note
Remainder after division; returns X - n.*Y where n = fix(X./Y). If Y is not an integer and the quotient X./Y is within roundoff error of an integer, then n is that integer. The inputs X and Y must be real arrays of the same size, or real scalars(provided Y ~=0).
Please note that:
uplus(a) Unary plus; increments by the amount a plus (a,b) Plus; returns a + b uminus(a) Unary minus; decrements by the amount a minus(a, b) Minus; returns a - b times(a, b) Array multiply; returns a.*b mtimes(a, b) Matrix multiplication; returns a* b rdivide(a, b) Right array division; returns a ./ b ldivide(a, b) Left array division; returns a. b mrdivide(A, B) Solve systems of linear equations xA = B for x mldivide(A, B) Solve systems of linear equations Ax = B for x power(a, b) Array power; returns a.^b mpower(a, b) Matrix power; returns a ^ b cumprod(A) Cumulative product; returns an array of the same size as the array A containing the cumulative product.-
If A is a vector, then cumprod(A) returns a vector containing the cumulative product of the elements of A.
-
If A is a matrix, then cumprod(A) returns a matrix containing the cumulative products for each column of A.
-
If A is a multidimensional array, then cumprod(A) acts along the first non-singleton dimension.
-
If A is a vector, then cumsum(A) returns a vector containing the cumulative sum of the elements of A.
-
If A is a matrix, then cumsum(A) returns a matrix containing the cumulative sums for each column of A.
-
If A is a multidimensional array, then cumsum(A) acts along the first nonsingleton dimension.
-
If X is a vector, then diff(X) returns a vector, one element shorter than X, of differences between adjacent elements: [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)]
-
If X is a matrix, then diff(X) returns a matrix of row differences: [X(2:m,:)-X(1:m-1,:)]
-
If A is a vector, then prod(A) returns the product of the elements.
-
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 an empty 0-by-0 matrix, prod(A) returns 1.
-
If A is a multidimensional array, then prod(A) acts along the first non-singleton dimension and returns an array of products. The size of this dimension reduces to 1 while the sizes of all other dimensions remain the same.
The prod function computes and returns B as single if the input, A, is single. For all other numeric and logical data types, prod computes and returns B as double.
prod(A,dim) Returns the products along dimension dim. For example, if A is a matrix, prod(A,2) is a column vector containing the products of each row. prod(___,datatype) multiplies in and returns an array in the class specified by datatype. sum(A)-
Sum of array elements; returns sums along different dimensions of an array. If A is floating point, that is double or single, B is accumulated natively, that is in the same class as A, and B has the same class as A. If A is not floating point, B is accumulated in double and B has class double.
-
If A is a vector, sum(A) returns the sum of the elements.
-
If A is a matrix, sum(A) treats the columns of A as vectors, returning a row vector of the sums of each column.
-
If A is a multidimensional array, sum(A) treats the values along the first non-singleton dimension as vectors, returning an array of row vectors.
sum(..., 'double')
sum(..., dim,'double')
Perform additions in double-precision and return an answer of type double, even if A has data type single or an integer data type. This is the default for integer data types.sum(..., 'native')
sum(..., dim,'native')
Perform additions in the native data type of A and return an answer of the same data type. This is the default for single and double. ceil(A) Round toward positive infinity; rounds the elements of A to the nearest integers greater than or equal to A. fix(A) Round toward zero floor(A) Round toward negative infinity; rounds the elements of A to the nearest integers less than or equal to A.idivide(a, b)
idivide(a, b,'fix')
Integer division with rounding option; is the same as a./b except that fractional quotients are rounded toward zero to the nearest integers. idivide(a, b, 'round') Fractional quotients are rounded to the nearest integers. idivide(A, B, 'floor') Fractional quotients are rounded toward negative infinity to the nearest integers. idivide(A, B, 'ceil') Fractional quotients are rounded toward infinity to the nearest integers. mod (X,Y)Modulus after division; returns X - n.*Y where n = floor(X./Y). If Y is not an integer and the quotient X./Y is within round off error of an integer, then n is that integer. The inputs X and Y must be real arrays of the same size, or real scalars (provided Y ~=0).
Please note
- mod(X,0) is X
- mod(X,X) is 0
- mod(X,Y) for X~=Y and Y~=0 has the same sign as Y
Remainder after division; returns X - n.*Y where n = fix(X./Y). If Y is not an integer and the quotient X./Y is within roundoff error of an integer, then n is that integer. The inputs X and Y must be real arrays of the same size, or real scalars(provided Y ~=0).
Please note that:
- rem(X,0) is NaN
- rem(X,X) for X~=0 is 0
- rem(X,Y) for X~=Y and Y~=0 has the same sign as X.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.