Azzera filtri
Azzera filtri

How to use command function?

2 visualizzazioni (ultimi 30 giorni)
gsourop
gsourop il 8 Nov 2016
Modificato: James Tursa il 8 Nov 2016
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.

Risposta accettata

James Tursa
James Tursa il 8 Nov 2016
Modificato: James Tursa il 8 Nov 2016
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.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by