draw linear regression lines and finding slope
3 views (last 30 days)
Show older comments
I have a very big data set(about 20,000,000 points)and matlab get crash when I want to draw linear regression line after plotting.How can I plot plyfit line and saving the result of slope?
ee= find (RVI>=0.8 & RVI <1.1);
x5 = Sh_new(ee);
y5 = TBh_new(ee);
figure; hold on;
scatter(x5,y5,'MarkerFaceColor','c','MarkerEdgeColor','c');
ylim([-30 30])
xlabel('Sigma _ hh - mean(Sigma_ hh) [db] ','Fontsize',20);
ylabel(' TB_h - mean(TB_h)[K]','FontSize',20);
0 Comments
Accepted Answer
Star Strider
on 2 Jan 2015
Edited: Star Strider
on 2 Jan 2015
You can do a linear regression without polyfit.
This works:
xy5 = sortrows([x5(:) y5(:)]); % Sorting For Smooth Plot
x5 = xy5(:,1);
y5 = xy5(:,2);
B = [ones(length(x5),1) x5(:)]\y5(:); % Estimate Regression Parameters
yf = [ones(length(x5),1) x5(:)]*B; % Calculate Regression Line
figure(1)
scatter(x5, y5)
hold on
plot(x5, yf, '-r')
hold off
EDIT — I forgot to identify the slope and intercept:
slope = B(2);
intercept = B(1);
2 Comments
Star Strider
on 3 Jan 2015
Edited: Star Strider
on 3 Jan 2015
My code worked correctly with some sample data I used to test it. (I test all the data I post, and if I cannot test it, specifically note that it is untested code.) I only tested the code I posted. I cannot test all your code because I don’t have your data.
NaN values usually result from either ‘0/0’ or similar calculations (that with good data, my code would not produce), or — most likely in this instance — from NaN values being in your data. If you have NaN values in your data, you have to eliminate them before you can calculate anything from your data. This means pairing them in a matrix (such as I did in my sort call) and then finding them (use isnan) and eliminating all rows with NaN values in them. However if the data you want to plot are the result of other calculations in your code, a better approach might be to go through your code and find out where the NaN values originated. That is your decision.
To eliminate the NaN values, use this before you sort your data:
xy5 = [x5(:) y5(:)];
[rnan,cnan] = find(isnan(xy5));
xy5(rnan,:) = [];
This will remove all the NaN values from your data, and any non-NaN values associated with them in the same row, leaving you with data you can use in the rest of my code. (I tested this code as well. It works.)
More Answers (0)
See Also
Categories
Find more on Testing Frameworks 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!