How to make a vector out of user input data?
4 views (last 30 days)
Show older comments
Hello,
I am working on a project and I'm trying to think of a way to take user input data and form it into a vector.
i.e. x=userinput('Enter a value between -∞ and ∞: ')
The user can then input as many values as they like. Then I would like these values to be put into a row vector.
My ultimate goal is to calculate the mean and variance of this data set, but we are not allowed to use the the mean() and var() commands.
Is this possible? I know there is the std() command that can be used with a vector, then to get the variance I will just square that value.
Thanks for any help you may provide.
I have attached a screenshot of the project just to clarify my words if they don't make sense.
~

Collin
2 Comments
Answers (1)
Stephen23
on 17 Feb 2016
Edited: Stephen23
on 17 Feb 2016
pmt = 'Enter a finite number value (or any letter to exit): ';
vec = [];
while nnz(isfinite(vec))<2 || isfinite(vec(end))
vec(end+1) = str2double(input(pmt,'s')); %#ok<SAGROW>
end
vec = vec(isfinite(vec));
myMean = sum(vec)/numel(vec);
myVar = std(vec)^2;
Keep in mind that your tutors really do read forums like this one, and that copying other peoples work without reference is called plagiarism.
See Also
Categories
Find more on Loops and Conditional Statements 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!