How to use command function?

조회 수: 7 (최근 30일)
gsourop
gsourop 2016년 11월 8일
편집: James Tursa 2016년 11월 8일
Hi everyone,
I have to generate 15 matrices of dimensions 432x6. Instead of doing it manually, such as:
%create a matrix with the forecasted values of all currencies for the 1 predictor.
for i=1:6;
for t=1:432;
predictor_1(t,i)=k1_total(t,1+15*(i-1));
end;
end;
%create a matrix with the forecasted values of all currencies for the 2 predictor.
for i=1:6;
for t=1:432;
predictor_2(t,i)=k1_total(t,2+15*(i-1));
end;
end;
%create a matrix with the forecasted values of all currencies for the 3 predictor.
for i=1:6;
for t=1:432;
predictor_3(t,i)=k1_total(t,3+15*(i-1));
end;
end;
and so on. k1_total is a 432x90 matrix. How could I use the ''function'' command in order to avoid all these replications and look more coherent.
I 've tried the following:
for i=1:size(k1_total,2);
[predictor_i,xxx,xxx,xxx,xxx,xxx,xxx]=Create_predictors(...
k1_total(:,i));
results_ECON(i,:)=[predictor_i];
end;
and on the script, I use:
function [predictor_i,SR,weight_risky,cumulative_return,avg_turnover,Sortino,G]=...
Create_predictors(k1_total,ir_i,target_volatility,actual_risky_return_i,ir_us_lag,k1_uk)
for i=1:6;
for t=432;
predictor_k_econ(t,i)=k1_total(t,1+15*(i-1));
end;
end;
But I get an error of mismatching dimensions. I would appreciate any help. Thanks in advance.

채택된 답변

James Tursa
James Tursa 2016년 11월 8일
편집: James Tursa 2016년 11월 8일
I would advise using a cell array for this instead of many separately named variables. E.g.,
predictor = cell(15,1);
for k=1:15
predictor{k} = k1_total(:,k:15:end);
end
So predictor{1} would be used downstream instead of your predictor_1, etc. This has the advantage of using indexing downstream in your code instead of hard-coded named variables.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Regression에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by