如何使用函数保存double for循环表? MATLAB

时间:2018-09-14 08:04:26

标签: matlab for-loop

我在网上发现了滚动窗口功能,

function output = createRollingWindow(vector, n)
% CREATEROLLINGWINDOW returns successive overlapping windows onto a vector
%   OUTPUT = CREATEROLLINGWINDOW(VECTOR, N) takes a numerical vector VECTOR
%   and a positive integer scalar N. The result OUTPUT is an MxN matrix,
%   where M = length(VECTOR)-N+1. The I'th row of OUTPUT contains
%   VECTOR(I:I+N-1).
l = length(vector);
m = l - n + 1;
output = vector(hankel(1:m, m:l));
end

通过此功能,我试图在代码中包含两个for循环,

%Out of sample attempt
clear;
clc;
kk=1;%to help with recording
for q = 1:3;
    for w = 1:6;
    data(:,1) = [ 0.824178794   0.320861779 0.544464994 0.443415606 0.881589471 0.126930695 0.10758006  0.64661864  0.186043513 0.113938139 0.397832754 0.245128803 0.601561899 0.702804161 0.868598731 0.896887742 0.315118703 0.652750644 0.442618269 0.414916628 0.091210459 0.778660087 0.477795261 0.170882616 0.358117319 0.952619704 0.299335988 0.672694952 0.659587974 0.116092814 0.354043427 0.558700934 0.383720179 0.497181625 0.220384304 0.918564103 0.83692923  0.091426279 0.748032692 0.632857627 0.711075772 0.116125763 0.597741796 0.514174441 0.615460425 0.645151999 0.226984236 0.12237818  0.699570062 0.114368326 0.846111165 0.695035952 0.23948643  0.599922736 0.306768402 0.067744491 0.783072298 0.087849296 0.85992126  0.242326707 0.707444842 0.708394112 0.648583229 0.881334696 0.822010696 0.482946533 0.973389017 0.99451342  0.396172299 0.885790819 0.6547881   0.435092468 0.982999085 0.082579592 0.282836873 0.281771702 0.659658085 0.628989875 0.653124888 0.158068163 0.72719761  0.204252699 0.417000233 0.883864326 0.54291388  0.923703895 0.092945717 0.635086879 0.849531721 0.235541373 0.37923794  0.104290602 0.012590039 0.487503652 0.199704805 0.594050564 0.9801534   0.978849296 0.187787141 0.964071808 0.139609956 0.814162483 0.30894139  0.812033217 0.142302294 0.055008992 0.400725294 0.619958465 0.228309428 0.606736617 0.065623529 0.987751722 0.88678899  0.693903646 0.89933127  0.4640739   0.96584424  0.644626537 0.701417614 0.586577243 0.984983184 0.141416031 0.348001281 0.646667949 0.696233019 0.40711813];
    data(:,2) = [0.601312801    0.567678926 0.010228683 0.819126718 0.374199446 0.752227144 0.692009283 0.160297793 0.41729925  0.004383375 0.391805283 0.727950395 0.042412421 0.702262491 0.46841405  0.335453208 0.846544411 0.022669174 0.93789211  0.628081132 0.922873091 0.794009152 0.398190721 0.146860091 0.276906047 0.040990629 0.885172725 0.995618555 0.081377375 0.673312607 0.507802794 0.507211426 0.864098858 0.618331858 0.497025625 0.199872624 0.680890254 0.733198925 0.073351847 0.535303576 0.037211143 0.256505612 0.076706486 0.209499708 0.20674952  0.591840692 0.816632191 0.870881906 0.795427041 0.206113399 0.383909285 0.917927405 0.868804777 0.839985866 0.346596048 0.750367231 0.960418228 0.001626573 0.613545782 0.343902168 0.011611464 0.516205321 0.21196681  0.885751081 0.415271144 0.621481833 0.791477412 0.894585662 0.79669597  0.056652307 0.914691669 0.567923067 0.086743503 0.384397699 0.795918739 0.339847836 0.96086507  0.218898901 0.593647285 0.861695814 0.210231592 0.899967064 0.897346931 0.330684285 0.110565195 0.878917027 0.32734661  0.997426716 0.766167074 0.993337315 0.454428456 0.835946458 0.69975433  0.983277113 0.868033073 0.551361664 0.565948465 0.383959861 0.915339748 0.165000585 0.598434963 0.469557418 0.997527846 0.731639315 0.074477013 0.829399281 0.72211419  0.110424513 0.942230634 0.031584519 0.844465109 0.117074378 0.093737687 0.609404299 0.807640005 0.048202653 0.782269274 0.659084731 0.488254063 0.032889558 0.947366815 0.787888171 0.456713783 0.155336211 0.93715265  0.515620832];
    data(:,3) = [0.601312801    0.567678926 0.010228683 0.819126718 0.374199446 0.752227144 0.692009283 0.160297793 0.41729925  0.004383375 0.391805283 0.727950395 0.042412421 0.702262491 0.46841405  0.335453208 0.846544411 0.022669174 0.93789211  0.628081132 0.922873091 0.794009152 0.398190721 0.146860091 0.276906047 0.040990629 0.885172725 0.995618555 0.081377375 0.673312607 0.507802794 0.507211426 0.864098858 0.618331858 0.497025625 0.199872624 0.680890254 0.733198925 0.073351847 0.535303576 0.037211143 0.256505612 0.076706486 0.209499708 0.20674952  0.591840692 0.816632191 0.870881906 0.795427041 0.206113399 0.383909285 0.917927405 0.868804777 0.839985866 0.346596048 0.750367231 0.960418228 0.001626573 0.613545782 0.343902168 0.011611464 0.516205321 0.21196681  0.885751081 0.415271144 0.621481833 0.791477412 0.894585662 0.79669597  0.056652307 0.914691669 0.567923067 0.086743503 0.384397699 0.795918739 0.339847836 0.96086507  0.218898901 0.593647285 0.861695814 0.210231592 0.899967064 0.897346931 0.330684285 0.110565195 0.878917027 0.32734661  0.997426716 0.766167074 0.993337315 0.454428456 0.835946458 0.69975433  0.983277113 0.868033073 0.551361664 0.565948465 0.383959861 0.915339748 0.165000585 0.598434963 0.469557418 0.997527846 0.731639315 0.074477013 0.829399281 0.72211419  0.110424513 0.942230634 0.031584519 0.844465109 0.117074378 0.093737687 0.609404299 0.807640005 0.048202653 0.782269274 0.659084731 0.488254063 0.032889558 0.947366815 0.787888171 0.456713783 0.155336211 0.93715265  0.515620832];
%This is what we are trying to forecast
createRollingWindow(data(:,q), w);%This uses the function saved as createRollingWindow
sum(ans,2); %This sums the rows in the matrix, which is what I need
%YF(kk,:)=(ans);
%kk=kk+1;

    end
end

我理想地需要一个向量,该向量包括for循环的结果

我试图在上面的代码中将此向量称为YF

我的问题是...

如何将两个循环的输出存储在称为YF的矩阵中

即每个滚动窗口为1:6,每个数据集为1:3。

谢谢!

尝试中建议的更新代码仍然需要第一个功能

%Out of sample attempt
clear;
clc;
%kk=1;%to help with recording
YF = [];
for q = 1:3;
    for w = 1:6;
    data(:,1) = [ 0.824178794   0.320861779 0.544464994 0.443415606 0.881589471 0.126930695 0.10758006  0.64661864  0.186043513 0.113938139 0.397832754 0.245128803 0.601561899 0.702804161 0.868598731 0.896887742 0.315118703 0.652750644 0.442618269 0.414916628 0.091210459 0.778660087 0.477795261 0.170882616 0.358117319 0.952619704 0.299335988 0.672694952 0.659587974 0.116092814 0.354043427 0.558700934 0.383720179 0.497181625 0.220384304 0.918564103 0.83692923  0.091426279 0.748032692 0.632857627 0.711075772 0.116125763 0.597741796 0.514174441 0.615460425 0.645151999 0.226984236 0.12237818  0.699570062 0.114368326 0.846111165 0.695035952 0.23948643  0.599922736 0.306768402 0.067744491 0.783072298 0.087849296 0.85992126  0.242326707 0.707444842 0.708394112 0.648583229 0.881334696 0.822010696 0.482946533 0.973389017 0.99451342  0.396172299 0.885790819 0.6547881   0.435092468 0.982999085 0.082579592 0.282836873 0.281771702 0.659658085 0.628989875 0.653124888 0.158068163 0.72719761  0.204252699 0.417000233 0.883864326 0.54291388  0.923703895 0.092945717 0.635086879 0.849531721 0.235541373 0.37923794  0.104290602 0.012590039 0.487503652 0.199704805 0.594050564 0.9801534   0.978849296 0.187787141 0.964071808 0.139609956 0.814162483 0.30894139  0.812033217 0.142302294 0.055008992 0.400725294 0.619958465 0.228309428 0.606736617 0.065623529 0.987751722 0.88678899  0.693903646 0.89933127  0.4640739   0.96584424  0.644626537 0.701417614 0.586577243 0.984983184 0.141416031 0.348001281 0.646667949 0.696233019 0.40711813];
    data(:,2) = [0.601312801    0.567678926 0.010228683 0.819126718 0.374199446 0.752227144 0.692009283 0.160297793 0.41729925  0.004383375 0.391805283 0.727950395 0.042412421 0.702262491 0.46841405  0.335453208 0.846544411 0.022669174 0.93789211  0.628081132 0.922873091 0.794009152 0.398190721 0.146860091 0.276906047 0.040990629 0.885172725 0.995618555 0.081377375 0.673312607 0.507802794 0.507211426 0.864098858 0.618331858 0.497025625 0.199872624 0.680890254 0.733198925 0.073351847 0.535303576 0.037211143 0.256505612 0.076706486 0.209499708 0.20674952  0.591840692 0.816632191 0.870881906 0.795427041 0.206113399 0.383909285 0.917927405 0.868804777 0.839985866 0.346596048 0.750367231 0.960418228 0.001626573 0.613545782 0.343902168 0.011611464 0.516205321 0.21196681  0.885751081 0.415271144 0.621481833 0.791477412 0.894585662 0.79669597  0.056652307 0.914691669 0.567923067 0.086743503 0.384397699 0.795918739 0.339847836 0.96086507  0.218898901 0.593647285 0.861695814 0.210231592 0.899967064 0.897346931 0.330684285 0.110565195 0.878917027 0.32734661  0.997426716 0.766167074 0.993337315 0.454428456 0.835946458 0.69975433  0.983277113 0.868033073 0.551361664 0.565948465 0.383959861 0.915339748 0.165000585 0.598434963 0.469557418 0.997527846 0.731639315 0.074477013 0.829399281 0.72211419  0.110424513 0.942230634 0.031584519 0.844465109 0.117074378 0.093737687 0.609404299 0.807640005 0.048202653 0.782269274 0.659084731 0.488254063 0.032889558 0.947366815 0.787888171 0.456713783 0.155336211 0.93715265  0.515620832];
    data(:,3) = [0.601312801    0.567678926 0.010228683 0.819126718 0.374199446 0.752227144 0.692009283 0.160297793 0.41729925  0.004383375 0.391805283 0.727950395 0.042412421 0.702262491 0.46841405  0.335453208 0.846544411 0.022669174 0.93789211  0.628081132 0.922873091 0.794009152 0.398190721 0.146860091 0.276906047 0.040990629 0.885172725 0.995618555 0.081377375 0.673312607 0.507802794 0.507211426 0.864098858 0.618331858 0.497025625 0.199872624 0.680890254 0.733198925 0.073351847 0.535303576 0.037211143 0.256505612 0.076706486 0.209499708 0.20674952  0.591840692 0.816632191 0.870881906 0.795427041 0.206113399 0.383909285 0.917927405 0.868804777 0.839985866 0.346596048 0.750367231 0.960418228 0.001626573 0.613545782 0.343902168 0.011611464 0.516205321 0.21196681  0.885751081 0.415271144 0.621481833 0.791477412 0.894585662 0.79669597  0.056652307 0.914691669 0.567923067 0.086743503 0.384397699 0.795918739 0.339847836 0.96086507  0.218898901 0.593647285 0.861695814 0.210231592 0.899967064 0.897346931 0.330684285 0.110565195 0.878917027 0.32734661  0.997426716 0.766167074 0.993337315 0.454428456 0.835946458 0.69975433  0.983277113 0.868033073 0.551361664 0.565948465 0.383959861 0.915339748 0.165000585 0.598434963 0.469557418 0.997527846 0.731639315 0.074477013 0.829399281 0.72211419  0.110424513 0.942230634 0.031584519 0.844465109 0.117074378 0.093737687 0.609404299 0.807640005 0.048202653 0.782269274 0.659084731 0.488254063 0.032889558 0.947366815 0.787888171 0.456713783 0.155336211 0.93715265  0.515620832];
%This is what we are trying to forecast
createRollingWindow(data(:,q), w);%This uses the function saved as createRollingWindow
sum(ans,2); %This sums the rows in the matrix, which is what I need
YF = [YF, ans]
%kk=kk+1;

    end
end

0 个答案:

没有答案