Vectorizing a recursive for loop avoiding numerical underflow

조회 수: 1 (최근 30일)
Josh Parks
Josh Parks 2020년 3월 23일
댓글: Josh Parks 2020년 3월 24일
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
Sriram Tadavarty 2020년 3월 24일
The last loop can be removed directly with
alpha(:,t) = c(t)*alpha(:,t);
Josh Parks
Josh Parks 2020년 3월 24일
Thanks, I saw the last one, the recursion is the part that's confusing me
And yes, these are right, they're getting added to each inner loop iteration, just forgot to initialize :)
%alpha_1
for i = 1:N
alpha(i,1) = pi(i)*B(1,O(1));
c(1) = c(1) + alpha(i,1);
end
%scale alpha_1
c(1) = 1/c(1);
for i = 1:N
alpha(i,1) = c(1)*alpha(i,1);
end

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by