FOR LOOP ERROR IN MATLAB

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

Answers (1)

[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 Comments

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
Thanks a lot, it is working now

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Products

Asked:

on 9 Nov 2021

Commented:

on 10 Nov 2021

Community Treasure Hunt

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

Start Hunting!