Be the first to rate this file! 4 Downloads (last 30 days) File Size: 2.69 KB File ID: #28333

Parameter Iterator (paramiterator)

by Matt Caywood

 

29 Jul 2010 (Updated 05 Aug 2010)

Allows complex arbitrarily nested loops to be flattened into a simple single loop.

| Watch this File

File Information
Description

Flattens arbitrarily nested loops into a single loop that iterates over all combinations of loop parameters.

Makes it very easy to change the iteration parameters without rewriting loops, reindenting code, updating indices, etc.

Very convenient for simulation problems where the parameters being tested frequently change, but the computation stays the same.

Nested loop code like this:

avalues = [1 2]; bvalues = [10 20]; cvalues = [100 200 300];
for ai = 1:length(avalues)
    for bi = 1:length(bvalues)
        for ci = 1:length(cvalues)
            scoretable(ai,bi,ci) = avalues(ai) + bvalues(bi) + cvalues(ci);
        end
    end
end

Can be flattened into this, and any parameter can be added or removed by changing only paramvariables and paramvalues:

paramvariables = {'A' 'B' 'C'};
paramvalues = { {1 2} {10 20} {100 200 300}};
piter = paramiterator(paramvariables,paramvalues);
for i = 1:length(piter)
    setvalues(piter,i);
    scorelist(i) = A + B + C;
end
scoretable = listtotable(piter,scorelist);

Results can be easily displayed with displaytable (http://www.mathworks.com/matlabcentral/fileexchange/27920-displaytable).

You can now test whether a parameter has changed since the last iteration, before running some computation. This is like nesting the computation within only some of the loops, making paramiterator a full replacement for nested loops.

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
02 Aug 2010 Jan Simon

It would be helpful to show the example in the description here. Then I do not have to download before I can understand, what the function does.

03 Aug 2010 Matt Caywood

Good idea, Jan. I've added an example. Hope you find the code useful.

Please login to add a comment or rating.
Updates
03 Aug 2010

added example to description

05 Aug 2010

added parameter change testing

Tag Activity for this File
Tag Applied By Date/Time
simulation Matt Caywood 30 Jul 2010 10:28:38
control Matt Caywood 30 Jul 2010 10:28:38
loop Matt Caywood 30 Jul 2010 10:28:38
index Matt Caywood 30 Jul 2010 10:28:38
flattening Matt Caywood 30 Jul 2010 10:28:38
table Matt Caywood 30 Jul 2010 10:28:38
indices Matt Caywood 30 Jul 2010 10:28:38
nested Matt Caywood 05 Aug 2010 10:46:52

Contact us at files@mathworks.com