필터 지우기
필터 지우기

Store result of three nested for loops

조회 수: 1 (최근 30일)
Stef
Stef 2018년 7월 2일
댓글: Rik 2018년 7월 3일
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
Stef
Stef 2018년 7월 2일
accuracy is just a simple calculation of other variables included, but it varies depending on the different variables I gave you
Stef
Stef 2018년 7월 2일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Rik
Rik 2018년 7월 2일
You could also use ndgrid to generate all combinations of loop parameters, and then use arrayfun to calculate your outputs.
  댓글 수: 11
Rik
Rik 2018년 7월 3일
편집: Rik 2018년 7월 3일
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
Rik
Rik 2018년 7월 3일
Did my suggestions help you? If so, please consider marking it as accepted answer. If not, feel free to comment with your remaining issues.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by