operations on specific row

조회 수: 1 (최근 30일)
gianluca
gianluca 2011년 11월 17일
Hi,
I've a for example a 5x3 matrix
2015 52 7
2015 53 12
2015 54 20
3096 77 7
3096 83 11
and I want apply the function p = polyfit(x,y,1) to third (independent variable) and second (dependent variable) column. Moreover I want apply the regressions to data which have the same value in the first column. In this case a first regression on the first 3 data and a second regression on the last 2 data. How can I define this criteria?
Thanks
Gianluca

채택된 답변

Mitch Martelli
Mitch Martelli 2011년 11월 17일
Hi,
for your first question you can use :
p = polyfit(A(:,3),A(:,2),1);
Pay attention to use one like degree .
For the second question the following code should work , but i dont know if there is a way to dont use for loop:
b = unique(A(:,1));
degree=1;
p=zeros(size(b,1),degree+1);
for i=1:size(b,1)
ind = find(A(:,1)==b(i));
p(i,:) = polyfit(A(ind,3),A(ind,2),1);
end
  댓글 수: 1
gianluca
gianluca 2011년 11월 17일
Hi Mitch,
your code works perfectly.
Thank you for your help and quickness!
Best regards
Gianluca

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Regression에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by