function states = markov_1(time, transmat)
format long
transmat = [0.9972 0.00022831; 0.5 0.5];
n = length(transmat);
states = zeros(1, n);
states(1)=1;
for i = 1:time
states = states * transmat;
end

Antworten (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 10 Nov. 2021

0 Stimmen

[for end] loop is working perfectly ok, but there is an error in your defined function's input arguments. See the corrections:
time =3;
transmat = [0.9972 0.00022831; 0.5 0.5];
markov_1(time, transmat)
function states = markov_1(time, transmat)
format long
%transmat = [0.9972 0.00022831; 0.5 0.5];
n = length(transmat);
states = zeros(1, n);
states(1)=1;
for i = 1:time
states = states * transmat;
end
end

3 Kommentare

Matthew Igbinehi
Matthew Igbinehi am 10 Nov. 2021
Thanks a lot for the response and help. It works this way but I am trying to creat a reusable function where I can assign new values of time from the command line and get an output. Something like "markov_1(87600, transmat). How do I do this?
The code @Sulaymon Eshkabilov posted will already work for user-provided time, if you write the code to markov_1.m
function states = markov_1(time, transmat)
n = length(transmat);
states = zeros(1, n);
states(1)=1;
for i = 1:time
states = states * transmat;
end
end
Matthew Igbinehi
Matthew Igbinehi am 10 Nov. 2021
Thanks a lot, it is working now

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu App Building finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 9 Nov. 2021

Kommentiert:

am 10 Nov. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by