In MatLab Write a function called sumAndAverage. It should take in an array of n
ID: 669005 • Letter: I
Question
In MatLab
Write a function called sumAndAverage. It should take in an array of numbers and return the sum and average of the array in that order. For example:
[s a] = sumAndAverage([3 2 3 2]) %should return 10 and 2.5
[s a] = sumAndAverage([5 -5 2 8 0]) %should return 10 and 2
[s a] = sumAndAverage([]) %should return 0 and 0
You can use Built-in functions.
Create a vector, then invoke your function like this:
>>Arr=[3 2 3 2]
>>[s a]=sumAndAverage(Arr)
Note: the function has only one parameter. It is a vector.
Explanation / Answer
working matlab code
result = zeros(size(g1)); % initialize sum
for ii = 1:n
eval(['result = result + g' num2str(ii) ';'])
end
gavg = mean(g1,n);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.