Reputation: 3
I need to vectorize this for loop to speed it up and I'm not sure how to do it.
for k=1:n
x2=x(k)*x(k);
y(k) = (1-c1*x(k)+c2*x2-(x(k)/60)*x2)/...
(1+c3*x(k)+c4*x2);
end
Upvotes: 0
Views: 44
Reputation: 19689
Element-wise power (or multiplication) and division is all what you need. I replaced your multiplications of x(k)
with itself with exponentials.
y = (1 - c1*x + c2*x.^2 - x.^3/60) ./ (1 + c3*x + c4*x.^2); % assuming n = numel(x)
% if n ≠ numel(x) then replace all 'x's in above line with x(1:n)
Upvotes: 1