Currently I have a parfor loop making calculations on a struct in MATLAB. While my code is a bit long to post, it can be emulated using the following example:
a.test = [1 2 3];
result = [];
parfor i = 1:3
c = a;
c.test(2) = round( rand() );
if c.test(2) == 1
%# Store c in result
end
end
disp(result.test) %# Should show [1 1 3]
a (and consequently c) is a very large structure, so storing every iteration is not feasible for me (due to memory constraints).
Ideally I would like to be able to store c straight to a variable that I initialise before the parfor loop. Looking at MATLAB’s Example: Using a Custom Reduction Function, I can see it is possible to store a given iteration variable (without having to store every single iteration) yet I do not fully understand their method. I’m baffled by how their function produces 2 separate output variables (seemingly) while the function only defines one output variable.
I am sure this is a very common problem but so far none of my searches have yielded any valid results.
Any help would be much appreciated.
The example you link to is not quite what you want. However, it is possible to store only the “good” results. Here’s a simple example: