How to make a vector out of user input data?
Afficher commentaires plus anciens
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
Réponses (1)
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.
1 commentaire
Collin Kinder
le 17 Fév 2016
Catégories
En savoir plus sur Data Import and Analysis dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!