[MATLAB] Write a function called myMultProd.m that computes the cumulative product of the elements in a vector. The cumulative product, pj, of the jth element of the vector x, xj, is defined by pj = (x1)(x2) … (xj) for j = 1:length of the vector x. DO NOT USE CUMPROD For example, when you run your function it should look like this:
>> x = [2 3 4 2]; >> myMultProd(x) >> ans = 2 6 24 48 That is, the function returns: 2, 2*3, 2*3*4, 2*3*4*2 where 2 is p1, 2*3 is p2, 2*3*4 is p3, etc.
a) Do this first using two for loops to explicitly carry out the calculation element-byelement. The inner loop should accumulate the product and the other loop should move through the elements of the vector p.
b) Write a line of code to replace (but do not actually remove, see next part of the problem) the entire inner for loop by using the prod function.
c) Add another argument to myMultProd which can either be a 1 or a 2. If it’s a 1, use the procedure in part (a). If it’s a 2, instead use the single line of code from part (b). So now your code should work like this: >> myMultProd(x,2) The output should be the same for both methods. You can check to make sure the output is correct by using the cumprod function, which does the same thing as the function you just wrote.
d) Add checks to your function that produce error or warning messages to make sure that:
- the input vector is a vector of numbers and not any other data type
- that the numbers in the vector are real (and not imaginary)
- the input vector is not a matrix - if the input vector is an empty array, then the output should also be an empty array
- the second input is a 1 or 2
Please find required MATLAB code along with necessary details in comments below:
--------------------------------------------------------------------------------------------------------- main_script.m
clear all; clc; close all;
x = [2 3 4 2];
myMultProd1_product=myMultProd(x,1) % actual product computed using myMultProd arg=1
myMultProd2_product=myMultProd(x,2) % actual product computed using myMultProd arg=1
actual_product=cumprod(x) % actual product computed using comprod
------------------------------------------------------------------------------------------------------------------------------ myMultProd.m
function y=myMultProd(x,arg)
if ~isvector(x) % ensure that x is a vector
if isempty(x) % check if x is empty
y=[];
end
error('Not a vector input');
return;
end
if ~isreal(x) % ensure that x has no imaginary values
error('Inputs not real');
return;
end
if ~(arg==1 || arg==2) % ensure that arg is either 1 or 2
error('Second argument invalid');
return;
end
if arg==1 % if arg is 1, use the first method
for i=1:length(x)
prodv=x(i);
for j=1:i-1
prodv=prodv*x(j); % multiply all previous values
end
y(i)=prodv;
end
elseif arg==2 % if arg is 1, use the second method
for i=1:length(x)
prodv=x(i);
prodv=prodv*prod(x(1:i-1)); % multiply all previous values
y(i)=prodv;
end
end
end
=================================== SCREENSHOT OF CODE
================================ SAMPLE OUTPUT
[MATLAB] Write a function called myMultProd.m that computes the cumulative product of the elements in a...
Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...
3. Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-statements, possibly nested, to return the three elements of the vector as three scalar output-arguments in nondecreasino-roer , i.e., the first output argument equals the smallest element of the input vector and the last output argument equals the largest element. NOTE: Your function may NOT use any built-in functions, e.g., sort, min, max, median, etc. (5 points) (bonus question). Write a function called...
Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...
What would the function look like in MATLAB? Write a function called mysort that takes a 3-element vector as its sole arguments. It uses if statements, possibly nested, to return a 3-element vector with its elements in non-decreasing order, i.e., the first element in the returned vector equals the smallest element of the input vector and the last element equals the largest element in the input vector. NOTE: Your function should not use any built-in functions, e.g., sort, min, max,...
programing C,already write part of code (a) Write a function print.array that receives an array of real numbers and the number of el stored in the array. This function must display the elements of the array in order with each one on a line by itself in a field width of 7 with two decimal places. (See sample output. Recall the format specifier would be "%7.21f"). (b) Sometimes we want to treat an array as a mathematical vector and compute...
MATLAB: write a function that retuns logical true of vector or matrics or scaler is empty Write a function that retuns logical true of vector or matrics or scaler is empty %% P2: Check for an empty matrix % Write a function myIsEmpty which takes one input (scalar, vector, matrix, ...) % and returns logical true if the input is empty and false otherwise. % DO NOT USE MATLAB's isempty FUNCTION! % % Example: An input of [] should result...
Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as output, c, returns the number of zeros at the beginning of v (number of zero elements before any non-zero element). For example, for input (-5, 5, 11] the output would be 0. If the input is [0, 0, 3, 0, 0, 0], the output would be 2. If the input is [0, 0, 0, 0, 7, 4) the output would be 4. 5) Write...
Problem Summary This is a basic problem that uses a for-loop. Write a function called vector_multiply that takes two vectors of the same length as input arguments and returns one row vector as output Each component of the output vector is the product of the corresponding components in the two input vectors. However, this function must not use an array operation it must instead use a for-loop. For example, >>product - vector_multiply([1, 2, 3, 6], [9, 6, 4, 0]) product...
Matlab: Write a function called minimax that takes M, a matrix input argument and returns mmr, a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row. As a second output argument called mmm, it provides the difference between the maximum and minimum element in the entire matrix. See the code below for an example: >> A = randi(100,3,4) A = 66 94 75 18 4 68 40 71 85 76...
Question:Matrix Notation Interpreter (matrix elements to cell array of formatted character vectors) Matlab question Matrix Notation Interpreter (matrix elements to cell array of formatted character vectors) A system of linear algebraic equations can be writen in matrx notation by applying the definizion of matrix multiplication. For example, the linear system Can be writen as or where and x = 2 matrix input ror the variable A and a × T e function command dennes the output variable linearSystem Code has...