필터 지우기
필터 지우기

draw linear regression lines and finding slope

조회 수: 3 (최근 30일)
Hana
Hana 2015년 1월 2일
편집: Star Strider 2015년 1월 3일
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);

채택된 답변

Star Strider
Star Strider 2015년 1월 2일
편집: Star Strider 2015년 1월 2일
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
Hana
Hana 2015년 1월 3일
Thanks for you answer,Unfortunately the result of intercept,slope and B is NAN. can you please check the following code: ee= find (RVI>=0.8 & RVI <1.1); x5 = Sh_new(ee); y5 = TBh_new(ee); 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
slope = B(2); intercept = B(1);
xlabel('Sigma _ hh - mean(Sigma_ hh) [db] ','Fontsize',20); ylabel(' TB_h - mean(TB_h)[K]','FontSize',20);
Star Strider
Star Strider 2015년 1월 3일
편집: Star Strider 2015년 1월 3일
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.
See the documentation for nan and isnan for details.
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.)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Create and Run Performance Tests에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by