MATLAB: Storing data for “for loop”

for loop

Hi everyone,
for my L0 = [1 2 3 4 5], L10 = [2 3 4 5 6] L20 = [0 9 2 3 4] …. L200 = [2 3 4 5 6] (Up to 20 L values, just type random L for asking)
and x = [0:1:4]
I want to perform a polyfit function for 6th polynomial order for each L, with same x, but i do not want to each time type "polyfit(x,L0,6)" and then "polyfit(x,L20,6)
My question is : How can I make a for loop for this
I have tried L = [L0 L10 L20 … L200],
but this way, it will store all values in one vector.
Please help ASAP !!
Thanks in advance

Best Answer

Try something like this:
L0 = [1 2 3 4 5];
L10 = [2 3 4 5 6];
L20 = [0 9 2 3 4];
L = [L0; L10; L20];
x = 0:4;
n = size(L,2)-2;
for k = 1:size(L,1)
p(k,:) = polyfit(x,L(k,:),n);
end
This creates a matrix of row vectors in ā€˜Lā€™. Note that I set ā€˜nā€™ to be 2 less than the vector lengths. A 6-order polynomial might be appropriate for your actual data, although it will crash here. (Please re-consider fitting a 6-order polynomial anyway.)