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

26 次查看(过去 30 天)
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

采纳的回答

KSSV
KSSV 2020-9-18
编辑:KSSV 2020-9-18
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by