Solve in MATLAB Code a) Write a function fn_vecAnalysis that will receive a vect
ID: 3913335 • Letter: S
Question
Solve in MATLAB Code
a) Write a function fn_vecAnalysis that will receive a vector as an input argument, and will sort the vector in ascending order and return its average and median. Any built-in functions may be used, except the median function.
b) Write a script, VecAnalysis which receive the vector and call the function fn_vecAnalysis, then display the original vector, sorted result as well as its average and median.
c) Generate random 1 x 10 vector with real number in the range of [-10 10], then run the script VecAnalysis.
Explanation / Answer
function [avg,med] = fn_vecanalysis(vec)
vec = sort(vec);
avg = mean(vec);
n = length(vec);
mid = (n+1)/2;
med = (vec(floor(mid)) + vec(ceil(mid)))/2;
end
vec = randi([-10,10],1,10);
[avg,med] = fn_vecanalysis(vec);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.