Running a script multiple times and recording outputs for each of the runs separately

3 次查看(过去 30 天)
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.

回答(2 个)

Rik
Rik 2017-9-8
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.
  1 个评论
Uros Jovanovic
Uros Jovanovic 2017-9-8
编辑:Uros Jovanovic 2017-9-8
Could you explain a little bit, please? I need to run the whole code above for about 1000 times, don't know how to do that, and to record each of the matrices that I get from running the above code 1000 times. I mean, is there a faster way than just clicking on the execute button 1000 times?

请先登录,再进行评论。


Image Analyst
Image Analyst 2017-9-8
Try this:
for k = 1 : 1000
S{k}=main23(n);
end
  4 个评论
Uros Jovanovic
Uros Jovanovic 2017-9-9
Thanks, but now I need to transform these cell arrays into matrices. In other words, each of the cell array needs to be a matrix (for example s(1)=S{1}), any thoughts?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Transmitters and Receivers 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by