Using a loop to create polyfit values for a matrix of numbers

13 views (last 30 days)
Hi there, I'm trying to create a for loop to run the polyfit function for an array of numbers. The code I am currently using is:
for i=[1:11182];
x(i)=polyfit(Usable_Freq,Usable_Temp(i,:),1);
end
What I need the code to do is for each row of the Usable_Temp matrix, use the same row of Usable_freq and have polyfit run to return the values of this line.Using the above code when I run it, the following error returns: ???
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> Loop_grad at 3 x(i)=polyfit(Usable_Freq,Usable_Temp(i,:),1);
Any help to resolve this would be greatly appreciated.

Accepted Answer

Andrew Newell
Andrew Newell on 27 Jan 2011
You need to index Usable_Freq also, and polyfit returns two values for a first order fit. Try this:
N = 11182;
x = zeros(N,2); % good idea to preallocate
for i=1:N
x(i,:)=polyfit(Usable_Freq(i,:),Usable_Temp(i,:),1);
end
Note the loop syntax: you don't need the square brackets or the semicolon.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!