Vectorizing a recursive for loop avoiding numerical underflow
1 次查看(过去 30 天)
显示 更早的评论
Hi all,
I'm currently trying to vectorize a couple nested for loops that have a scaling coefficient (c) in there to prevent numerical underflow (note a(i,j) always less than 1). Any help is much appreciate!
Thanks,
Josh
N = 4;
T = 120000000;
M = 170;
O = randi(M,1,T);
B = zeros(N,M);
alpha = zeros(N,T);
A = rand(N);
B = rand(N,M);
c = zeros(1,T);
%compute alpha_t(i)
for t = 2:T
for j = 1:N
for i = 1:N
alpha(j,t) = alpha(j,t) + alpha(i,t-1)*A(i,j);
end %i
alpha(j,t) = alpha(j,t)*B(j,O(t));
c(t) = c(t) + alpha(j,t);
end %j
%scale alpha_t
c(t) = 1/c(t);
for j = 1:N
alpha(j,t) = c(t)*alpha(j,t);
end
end %t
3 个评论
Sriram Tadavarty
2020-3-24
The last loop can be removed directly with
alpha(:,t) = c(t)*alpha(:,t);
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Mathematics and Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!