How to iteratively multiply a vector by a matrix iteratively
조회 수: 3 (최근 30일)
이전 댓글 표시
I want to multiply an initial vector p_t0 by a matrix P. The vector and matrix are specified below. If I simply do p = p_t0*P I get my first set of answers which is what I want but I want to repeat the procedure 50 times (for t=[1 50]). Any ideas on how I can do this? I attempted it below but kept getting an error saying "the number of elements in A and B must be the same."
h = 0.5;
u = 0.3;
P = [1-h h 0 0 0 ; ...
u 1-h-u h 0 0; ...
0 u 1-h-u h 0; ...
0 0 u 1-h-u h; ...
0 0 0 u 1-u;];
%Initialize initial probability vector
p0_t0 = 0.2;
p1_t0 = 0.2;
p2_t0 = 0.2;
p3_t0 = 0.2;
p4_t0 = 0.2;
p_t0 = [p0_t0 p1_t0 p2_t0 p3_t0 p4_t0];
p = p_t0*P %initial vector produced at time of 0
tf = 50
for t = 2:tf+1
p(1) = p_t0*P
p(t) = p(t-1)*P;
end
댓글 수: 0
채택된 답변
Roger Stafford
2017년 2월 16일
The the product p_t0*P produces a five-element vector so you should write:
......
p = zeros(tf,5); % <-- for more efficient allocation
p(1,:) = p_t0*P;
for t = 2:tf
p(t,:) = p(t-1,:)*P;
end
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!