Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Matlab function called onlyoddprod.m which will take a vector of integer

ID: 3803189 • Letter: W

Question

Write a Matlab function called onlyoddprod.m which will take a vector of integers invest and return the product of only its odd positive entries as the value outfall. All negative entries, zero entries, and positive even entries will be ignored. If the vector contains no odd positive entries, then the function should simply return the value 1. You must use either a for loop or a while loop in you function to calculate the product. You are forbidden from using any other built-in Matlab functions as a part of your function. Function specifications and some sample function calls are given below. input parameter invest vector of integer values product of only odd positive entries input parameter invest output parameter outfall sample function calls onlyoddprod([1, 2, 3, 4, 5, 6, 7]) produces 105 onlyoddprod([-2, 3,0, 9, 4, -5]) produces 27 onlyoddprod([-8, -1,0, 2]) produces 1 onlyoddprod([2, 4, 6, 8, 10]) produces 1

Explanation / Answer

Matlab function onlyoddprod.m

function outval = onlyoddprod(invect)
% INPUT:   invect = vector of integer values
% OUTPUT: outval = product of only odd positive entries
N = length(invect); % taking the number of elements in invect
% We have to use the length() function of size() function to get the
% Number of elements in invect.
outval = 1; % initilize the variable outval
for k = 1:N % loop to get each elements of invect
    l = 1;P = 0; % Seting parameters for finding remainder mod(invect(k),2)
    while P <= invect(k)
        P = 2*l;
        l = l+1;
    end
    Rem = invect(k) - (P-2); % Finding mod(invect(k),2)
    if Rem == 1 && invect(k) > 0 % If Rem is one and invect(k) is not zero
        outval = outval*invect(k); % Performing the product od odd numbers
    end
end
end

OUTPUTS

>> invect = 1:7;
>> outval = onlyoddprod(invect)

outval =

   105

>> invect = [-2,3,0,9,4,-5];
>> outval = onlyoddprod(invect)

outval =

    27

>> invect = [-8.-1,0,2];
>> outval = onlyoddprod(invect)

outval =

     1

>> invect = [2,4,6,8,10];
>> outval = onlyoddprod(invect)

outval =

     1

>>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote