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.
cummax
function in their array, it only does one output (which happens to be a 2-element array). - Danicacummax
function I see it now. At first I ignored the A(1) and B(1) as formalities, but I see what it is doing now. You are write though it doesn't apply to me. Cancelling the other tasks isn't important, getting the result out is more important. Jonas below shows an easy way to do this, though. - rbhalla