How to iteratively multiply a vector by a matrix iteratively

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 16 Feb. 2017
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

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by