Closest number to an average
Show older comments
The script should assume a vector A is assigned in the command window. The
script should create a scalar x where x is the number in A that is closest
to the average. Commands mean, median, mode, sum, max, or min are not
allowed for this challenge.
An example would be
>> A = [-1 -0.5 0 1 4];
>> script
x =
1
I've made a code to get the average but I wasn't able to find something to make x be a number in A thats closest to my average:
total = 0;
for ii = A
total = total + ii;
average = total/length(A);
x =
end
1 Comment
Answers (1)
Image Analyst
on 2 Jun 2015
Edited: Image Analyst
on 2 Jun 2015
0 votes
Evidently this is a homework problem - something I didn't know when I gave a complete solution to LEP in http://www.mathworks.com/matlabcentral/answers/221740#answer_181151
But now I'll mark both questions as homework - something you should have done yourself.
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!