Run a m-file (which simulates a Markov chain) multiple times and store the results

I wrote an m-file (called Markov) with simulates a Markov chain and creates time-series for a number of variables on the basis of that particular history of shocks realized.
I need to run the code for 1,000 times and store the results, so that I can calculate some statistics such as the mean standard deviation of my variables.
Thanks in advance.

 Accepted Answer

Can't you simply use a for Loop like this one?
for i=1:1000
result{i} = markov(input{i});
end
% Then you can process the results here

6 Comments

But please with array preallocation!!! Otherwise you are learning bad habits right from the start. There is no reason why you should not learn how to write fast and efficient MATLAB code, and this most definitely includes cell array preallocation!
Also you should never use i or j for loop variables, as these are used for the inbuilt imaginary unit. For the same reason it is a very bad idea to use the variable name input, which shadows the inbuilt input function.
So a better loop would be:
result = cell(1,1000);
for k = 1:1000
result{k} = markov(arg1,arg2,..);
end
Thank you both for your answer. I copied and pasted your code into my m.file but it does not work. The error code is the following: Undefined variable "input" or class "input". I am quite new to Matlab, thanks for your help.
You need to call your function markov inside the loop (capitalized correctly). You need to provide the function with its inputs (if there are any). Because you did not tell us what inputs your function has we had to use some placeholder inputs. As I explained in my comment, inputs is a bad name for a variable.
Dear Stephen, thank you again for your answer. I have attempted several things, but the problem is the following: "Attempt to execute SCRIPT Markov as a function". Matlab reads my file as script, should I open a function file separately? I am a bit confused :(
If it can be of any help to other users, I managed to do it as follows. I opened another m-file (same directory) and wrote the following code:
result = zeros(1000,1000);
for k =1:1000
Markov
result(k,:)=nameofthevariable;
end
where the variable is a vector (1,1000).
It seems that you have written a script. Functions are better for various reasons (own workspace, function encapsulation, debugging). You can easily turn your script into a function by writing this on the first line:
function out = Markov
... your code here
where the variable out is the name of whatever variable you have define inside your code that you want the function to return. You can then call this function in a loop:
result = zeros(1000,1000);
for k =1:1000
result(k,:) = Markov();
end
Read more about the differences between functions and scripts here:
In general functions are a much more robust and a better way to write code :

Sign in to comment.

More Answers (0)

Asked:

on 19 Feb 2016

Edited:

on 22 Feb 2016

Community Treasure Hunt

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

Start Hunting!