Solving MATLAB Sum Loop Issue with 2,187 Data Points

Click For Summary
SUMMARY

The forum discussion addresses a MATLAB coding issue related to calculating the mean of the correlation coefficient (r) across multiple iterations. The user attempts to compute r five times using random data points from a dataset of 2,187 entries but encounters a problem with summing the results. The solution involves initializing a variable sum_r to zero before the loop and updating it with each calculated r value. Finally, the total sum is displayed after the loop concludes.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of correlation coefficients
  • Basic knowledge of random number generation in MATLAB
  • Experience with handling NaN values in datasets
NEXT STEPS
  • Explore MATLAB's nansum function for handling missing data
  • Learn about MATLAB's random number generation techniques
  • Investigate optimization techniques for loop performance in MATLAB
  • Study statistical methods for calculating means of multiple values
USEFUL FOR

This discussion is beneficial for MATLAB programmers, data analysts, and statisticians who are working with large datasets and need to compute statistical measures efficiently.

Tone L
Messages
72
Reaction score
7
So i want to calculate an r value 5 different times then find the mean of the 5 calculated values.
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 news on Phys.org
Set a variable sum_r =0; before all your code. Total sum_r = sum_r + r; right after r is calculated. Display sum_r after your code.
 
  • Like
Likes   Reactions: Tone L

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K