How to make a vector out of user input data?

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 commentaires

Jan
Jan le 17 Fév 2016
What have you tried so far?
This is what I've tried so far:
if true
% code
n=0;
for i=1:n
a(i)=input('Input a real number: ')
end
end
I'm just having a hard time thinking through it and expanding from here.
Once I'm able to generate the vector, I know what to do from there. It's just getting there that is the problem.

Connectez-vous pour commenter.

Réponses (1)

Stephen23
Stephen23 le 17 Fév 2016
Modifié(e) : Stephen23 le 17 Fév 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.

1 commentaire

Collin Kinder
Collin Kinder le 17 Fév 2016
Thank you. Unfortunately, we haven't learned most of those commands. And I definitely wasn't looking to just take the code. I was just more looking for guidance on writing my own.

Connectez-vous pour commenter.

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!

Translated by