Write this matlab function, using isnumeric (as stated in the question). The j-t
ID: 3792518 • Letter: W
Question
Write this matlab function, using isnumeric (as stated in the question).
The j-th cumulative product of a vector x = (x_1, x_2, ..., x_n), for j lessthanoreuqlato n is defined by P_j = x_1 middot x_2 middot middot middot x_j. The cumulative product vector P contains all of the cumulative products as its elements, i.e. P = [p_1 p_2 middot middot middot p_n] where n is the number of elements in the vector x. For example, the cumulative product vector P of the vector x= [3 5 8 1 2 7] is P = [3 15 120 120 240 1680] Write a MATLAB function calc_prod(x) that computes and returns the cumulative product vector of the vector x. You must make sure that the vector x contains only numbers and if not return an appropriate error message.Explanation / Answer
%matlab code
function P = calc_prod(x)
P = [];
product = 1;
for i=1:length(x)
product = product*x(i);
P(i) = product;
end
end
x = input('Enter vector x: ');
if isnumeric(x) == 1
disp('Cumulative Product');
calc_prod(x)
else
disp('Invalid Input');
end
%{
output:
Enter vector x: [3 5 8 1 2 7]
ans =
3 15 120 120 240 1680
%}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.