Any time you find yourself breaking a vector into separate variables in MATLAB, you're probably making more work for yourself. MATLAB is optimized for operations involving vectors and matrices, and using vectorization will often give you more efficient and concise code.
From your comments, it appears that you ultimately want to evaluate a polynomial y = a0 + a1*x + a2*x^2 + ... + am*x^m
, where your coefficients a0
through am
are the variables you are wanting to initialize from the column vector afit
. A better alternative would be to use vectorized operations to compute your polynomial using afit
directly:
y = sum(afit(1:(m+1)).*x.^(0:m).');
Here, we index afit
based on the value of m
and then multiply the resulting sub-vector element-wise by a value x
raised element-wise to the power given in the column vector (0:m).'
. The resulting vector of values is them summed using sum
to get the result y
.
As Ben points out, there is even a built-in function polyval
that can perform this evaluation for you. You just have to flip the order of elements in the sub-vector indexed from afit
:
y = polyval(flip(afit(1:(m+1)), 1), x);