How to make a vector out of user input data?

4 views (last 30 days)
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
Collin Kinder
Collin Kinder on 17 Feb 2016
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.

Sign in to comment.

Answers (1)

Stephen23
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.
  1 Comment
Collin Kinder
Collin Kinder on 17 Feb 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.

Sign in to comment.

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!