Info

This question is closed. Reopen it to edit or answer.

how to take a mean of an array with using for loops

1 view (last 30 days)
so i have to load data from a text file, choose the first column of data and find the mean while using for loops. So far I have this and then I am lost on how to find the mean of the data. I understand you can find the mean just by using the mean function but that is not what the homework question is asking
x=load('example.txt');
y=x(:,1);

Answers (1)

Star Strider
Star Strider on 1 Feb 2015
Edited: Star Strider on 1 Feb 2015
The mean is the sum of the elements in your vector, divided by the number of elements in it.
You would begin with:
x = load('example.txt');
len_x = length(x);
sum_x = 0;
for k1 = 1:len_x
... YOUR CODE HERE ...
end
mean_x = ... YOUR CODE HERE ...
I could fill those in, but then you wouldn’t have the fun of discovering your own programming talents!
Note: Please do not use ‘sum’ or ‘mean’ as variable names. Those are the names of built-in MATLAB functions, and using their names as variables will overshadow those functions.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!