Running a script multiple times and recording outputs for each of the runs separately
3 views (last 30 days)
Show older comments
Hi all,
I'm having a function (say A) that generates a random n by n matrix (I need the value of n=8) and I use those numbers to calculate a cost function (B). So I need to run the first function A multiple times (lets say 1000 times) giving me 1000 different 8 by 8 matrices of random numbers and then calculating the cost function using those 1000 different matrices.
Here is my A function:
function [S]=main23(n)
%// n - matrix size (in my case it's 8)
[ ii jj ] = ndgrid(1:n); %// rows and columns of S
ii = ii(:);
jj = jj(:);
success = 0; %// ...for now
attempt = 0; %// attempt count (not really needed)
while ~success
attempt = attempt + 1;
S = NaN(n, n); %// initiallize result. NaN means position not filled yet
t = 1; %// number t is being placed within S ...
u = 1; %// ... for the u-th time
mask = true(1, numel(ii)); %// initiallize mask of available positions
while any(mask) %// while there are available positions
available = find(mask); %// find available positions
r = randi(numel(available), 1); %// pick one available position
itu = ii(available(r)); %// row of t, u-th time
jtu = jj(available(r)); %// col of t, u-th time
S(itu, jtu) = t; %// store t at that position
remove = (ii==itu) | (jj==jtu);
mask(remove) = false; %// update mask of positions available for t
u = u+1; %// next u
if u > n %// we are done with number t
t = t+1; %// let's go with new t
u = 1; %// initiallize u
mask = isnan(S(:)); %// initiallize mask for this t
end
if t > n %// we are done with all numbers
success = 1; %// exit outer loop (inner will be exited too)
end
end
end
So I need this function to run 1000 times for example and to record each of the 1000 matrices. Any idea, please?
Thank you.
0 Comments
Answers (2)
Rik
on 8 Sep 2017
If your code takes a very long time you might go for saving each result to a separate mat file. This would also enable running the code concurrently on different machines, provided that they share some disk. (generate file names with something like sprintf('%sresult%05d.mat',currentPath,currentCount))
Another option is to save the results in one cell array.
See Also
Categories
Find more on Transmitters and Receivers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!