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

Risposte (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 Commenti

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

Accedi per commentare.

Categorie

Scopri di più su App Building in Centro assistenza e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by