Using MATLAB, You are designing a custom function to separate the even values in
ID: 1715599 • Letter: U
Question
Using MATLAB,
You are designing a custom function to separate the even values in a numeric vector u from the odd values in u. The results are stored in two separate vectors ev and ov. In addition, the function keeps a count of the total number of elements in u that are positive in value. Part 1: Write a custom function to perform the separation and counting. Your function must meet these specifications: Accept a single vector u as an input argument and return vectors ev, ov, and the scalar total count as output arguments. Use a for loop, array indexing, and one or more if or if-else selections to determine: if an element is even or odd if an element is positive Use either vector concatenation or indexing to build up the ev and ov vectors. (Even elements are stored in ev and odd elements are stored in ov) Use a counter variable to keep track of the total number of positive values. The function itself should not prompt the user for input, nor should it display anything. Include an H1 help line in your function. Part 2: Write a script named test. m to call your custom function. Your script must meet these specifications: Prompt the user to enter a vector. Use the input function to do this. Call your custom function and pass it the user's vector as an input argument. Display both output vectors and the total count that are returned by the custom function.Explanation / Answer
part 1 : %%% function name i have given as bubai
function [ev,ov] = bubai(u)
if u(mod(u,2)==0)
ev=u(mod(u,2)==0);
if u(mod(u,2)==1)
ov=u(mod(u,2)==1);
end
end
end
part 2 : %%% to test the function 'bubai' using some random values
x=input('Enter the values to form a matrix using []=' );
[ev,ov]=bubai(u);
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.