Using a loop to create polyfit values for a matrix of numbers
13 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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.
2 Comments
More Answers (0)
See Also
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!