MATLAB: How to go through each element of a vector without a for loop

MATLAB: How to go through each element of a vector without a for loop

for loopMATLAB

I would like to write the following code without a for loop. Thanks in advance!
function fh = poly_fun(p)
function polynomial = poly(x)
polynomial = 0;
for ii = 1:length(p)
polynomial = polynomial + p(ii) .* x.^(ii-1);
end
end
fh = @poly;
end

Best Answer

  • If P is an array of coefficients and x is the value
    n = length(p)-1 ;
    iwant = sum(p.*x.^(n:-1:0))
    If x is an column array
    iwant = sum(sum(p.*x.^(2:-1:0),2)) ;
    Read about polyval.
  • Add a Comment

    Your email address will not be published. Required fields are marked *