How to iteratively multiply a vector by a matrix iteratively

1 次查看(过去 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

采纳的回答

Roger Stafford
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 个)

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by