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

forlooppolyfit

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.

Best Answer

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.
Related Question