Store result of three nested for loops

I have a matrix X consisting out of variables of 50 period (loop 1). I want to do a forecast for different time horizons: 2,4,6,8 periods (loop 2). Due to the class imbalance problem of X I want to repeat a subsample (the small group) several times (variable i)(loop 3). I want to store the period, the forecasting horizon, the number of repetitions and the accuracy.
i = 10:10:100;
for t = 1:max(period)-8
for z = 1:4 %lag order
lag_order = 2*z
h = t + lag_order;
for n = 1:numel(i); % repetitions of the a submatrix of X
accuracy
end
end
end
Maybe there is even a better way to do it with a matrix instead of a for loop.

2 Comments

accuracy is just a simple calculation of other variables included, but it varies depending on the different variables I gave you
It´s 200 lines of code. So basically in the third loop there is a support vector machine whichs accuracy is evaluated for each combination of the loop parameters

Sign in to comment.

Answers (1)

Rik
Rik on 2 Jul 2018
You could also use ndgrid to generate all combinations of loop parameters, and then use arrayfun to calculate your outputs.

11 Comments

Does it work like this?
lag = 2:2:8
period = 1:42
repeat = 10:10:100
grid = ndgrid(period, repeat, lag)
No, like this:
lag = 2:2:8;
period = 1:42;
repeat = 10:10:100;
[grid_period, grid_repeat, grid_lag]= ndgrid(period, repeat, lag);
[grid_sum,grid_product]=arrayfun(@example_function,...
grid_period, grid_repeat, grid_lag);
function [out1,out2]=example_function(period,repeat,lag)
out1=period+repeat+lag;
out2=period*repeat*lag;
end
Thank you. If I make one big loop over the whole grid, what index does the loop then have?
for k = ?
...
end
You'll have to pre-allocate the outputs, and then use something like the code below. But why do that? arrayfun will either be a lot faster, or it will not make any difference.
lag = 2:2:8;
period = 1:42;
repeat = 10:10:100;
[grid_period, grid_repeat, grid_lag]= ...
ndgrid(period, repeat, lag);
[grid_sum,grid_product]=arrayfun(@example_function,...
grid_period, grid_repeat, grid_lag);
for k=1:numel(grid_period)
period=grid_period(k);%optional
repeat=grid_repeat(k);%optional
lag=grid_lag(k);%optional
%rest of your code
end
Because I have an SVM I want to test with different parameters. And I don´t know how to apply the arrayfun to such a complex procedure
You simply need to wrap your code in a function that takes a few inputs and returns a few outputs. The code complexity itself doesn't matter. My mini-example shows how to implement such a function. The example I used is of course very simple, but you can use the structure. Just make a function out of it.
Ok so the problem is that I have some variables depending on the grid like
X = [repmat(y grid_repeat(t),1)];
Then I have functions like fitcsvm that are included. So I think I can´t just make one line of arrayfun including 100 lines of code
Stephen23
Stephen23 on 3 Jul 2018
Edited: Stephen23 on 3 Jul 2018
@Stef: put the code into a function. Call that function in arrayfun. Just like Rik Wisselink showed.
There is no limit to how many lines of code your functions can have, whether local function, nested function, or main function ... all of these would allow you to have 100 lines or 10,000 lines of code (not that I would recommend doing that many in one function!).
Ok, say I put the whole for loop in an .m file. But in that file I have something like X(period==t) where t is any period. But sometimes I refer to period and sometimes I need the index of the lag order. I just don´t know how to bring that all in one file
That is just a question of inputs. What inputs does your function need? Does it need all three vectors and the current indices to them?
If memory is not an issue, you can wrap the vectors in a cell, and use repmat to duplicate the data. If that is too much overhead, you can set the vectors in a separate function. Both are shown below.
lag = 2:2:8;
period = 1:42;
repeat = 10:10:100;
[grid_period, grid_repeat, grid_lag]= ...
ndgrid(period, repeat, lag);
grid_period_vector=repmat({period},size(grid_period));
%just use grid_period_vector as another input to your arrayfun function
Option 2: call the function below to set the three vectors. You can call this to make your grids, and inside your arrayfun function.
function [lag,period,repeat]=set_lag_period_repeat
persistent lag_ period_ repeat_
if isempty(lag_)
lag_ = 2:2:8;
period_ = 1:42;
repeat_ = 10:10:100;
end
%persistent variables cannot be outputs themselves
[lag,period,repeat]=deal(lag_,period_,repeat_);
end
Did my suggestions help you? If so, please consider marking it as accepted answer. If not, feel free to comment with your remaining issues.

Sign in to comment.

Asked:

on 2 Jul 2018

Commented:

Rik
on 3 Jul 2018

Community Treasure Hunt

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

Start Hunting!