Solving MATLAB Sum Loop Issue with 2,187 Data Points

In summary, the conversation discussed the process of calculating an r value five times and then finding the mean of the five calculated values. The code was designed to generate 100 random data points between 1 and 2187, but it was discovered to have a bug. The main issue was that the code was looping through the calculation of r without a way to sum the values. To address this, it was suggested to set a variable sum_r to 0 before the code and then add the value of r to it after each calculation. Finally, the total sum_r would be displayed after the code.
  • #1
Tone L
73
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
  • #2
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 Tone L

1. How do I solve a MATLAB sum loop issue with a large number of data points?

The best way to solve this issue is to use vectorization instead of a for loop. This means using MATLAB's built-in functions to perform operations on arrays rather than looping through each data point.

2. Why is my MATLAB code running slowly when using a for loop to sum 2,187 data points?

For loops can be inefficient in MATLAB, especially when dealing with large amounts of data. It is recommended to use vectorization instead for faster and more efficient code execution.

3. How can I improve the speed and performance of my MATLAB code when dealing with a large number of data points?

In addition to using vectorization, you can also try preallocating arrays, avoiding unnecessary operations, and using built-in functions instead of custom functions.

4. Can I use parallel processing to speed up my MATLAB code when working with a large number of data points?

Yes, MATLAB has built-in support for parallel computing which can greatly improve performance when dealing with large datasets. You can use functions such as parfor to run code in parallel on multiple processors.

5. I am getting an "Out of Memory" error when trying to sum a large number of data points in MATLAB. How can I fix this?

This error is likely due to your computer's memory limitations. You can try using a computer with more RAM or try breaking your data into smaller chunks and summing them separately. Alternatively, you can also try using parallel processing to distribute the workload across multiple processors.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
901
  • Engineering and Comp Sci Homework Help
Replies
7
Views
886
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
3K
Back
Top