possible for such for-loop to be vectorized?

2 views (last 30 days)
Hello all, I'm attempting to speed up my for-loop which performs something akin to Gaussian elimination in a iterative way, since it has to be implemented in simulink, i went for for-loop instead of recursive function. Could anyone give me some hint on how to possibly vectorize it? Simplified code for illustration. Thanks in advance.
function Uopt = forloop(sq_r,Uunc,H) % where H is 3-by-3 lower triangular matrix,Uunc 3-by-1 vector
Uopt = NaN(3,1);
U = NaN(3,1);
for i = 0:1
U(1) = i;
sq_dnew = (Uunc(1) - H(1,1:1)*U(1:1))^2;
if sq_dnew <= sq_r
sq_d1 = sq_dnew;
for j = 0:1
U(2) = j;
sq_dnew = (Uunc(2) - H(2,1:2)*U(1:2))^2 + sq_d1;
if sq_dnew <= sq_r
sq_d2 = sq_dnew;
for k = 0:1
U(3) = k;
sq_dnew = (Uunc(3) - H(3,1:3)*U(1:3))^2 + sq_d2;
if sq_dnew <= sq_r
sq_r = sq_dnew;
Uopt = U;
end
end
end
end
end
end

Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!