operations on specific row

4 views (last 30 days)
gianluca
gianluca on 17 Nov 2011
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

Accepted Answer

Mitch Martelli
Mitch Martelli on 17 Nov 2011
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 Comment
gianluca
gianluca on 17 Nov 2011
Hi Mitch,
your code works perfectly.
Thank you for your help and quickness!
Best regards
Gianluca

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!