So i want to calculate an r value 5 different times then find the mean of the 5 calculated values.(adsbygoogle = window.adsbygoogle || []).push({});

I have 2,187 data points. the first line of code generates 100 random points 1-2187.

The code has a bug but my major issue is it calculates r then loops again calculates r, loops again calculates r and there is no way to sum this. How can I edit my code.

for i = 1:5

randomdata = round(rand(100,1).*2187);

xval = x(randomdata);

yval = y(randomdata);

numerator = length(xval)*nansum(xval.*yval) - (nansum(xval)*nansum(yval));

xsumsquared = nansum(xval)^2;

ysumsquared = nansum(yval)^2;

dddx = sqrt(length(xval)*nansum(xval.^2) - xsumsquared);

ddy = sqrt(length(xval)*nansum(yval.^2) - ysumsquared);

demonator = dddx * ddy;

r = numerator/demonator;

end

thanks

**Physics Forums | Science Articles, Homework Help, Discussion**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# MATLAB sum loop issue

Tags:

**Physics Forums | Science Articles, Homework Help, Discussion**