Overwrite a CELL-ARRAY

6 views (last 30 days)
DINI
DINI on 1 Jul 2019
Commented: DINI on 1 Jul 2019
Hello everyone!
I have a BIG problem, and I dont know how to fix it! I have 3 for-loops (iside each other), the final result should be written in a cell array, every column in each cell is written loop by loop, I mean, in total I have 4 cells (thats works perfectly). The first 1 columns of each cell are written perfectly, but when its time to calculate the second column of the first cell, the code overwrite the first column of the first cell with the first column of the 4th cell, and that happens successively.
I copy a part of the code
for j = 1: predecir
for i=1:puntos_grilla
for itera=1:cantidad_de_iteraciones
variable = vtec_logaritmico{i};
T = tonndata(variable(ndiaini+j:delta-1+j,1),false,false);
.
.
.
.
.
.
.
vtec_pronosticado(itera,j)= exp(yys(end))*vtec_intervalo(ivtec+j-1,1);
vtec_final{1,i} = vtec_pronosticado ; %Cell-Array con n cantidad de columnas como n iteraciones tengamos
end
end
end
Cold you give me a hand?
Do you need all the code and the data?
  2 Comments
Geoff Hayes
Geoff Hayes on 1 Jul 2019
Dini - you may need to show more of your code. Also, with this line
vtec_final{1,i} = vtec_pronosticado
shouold j be used? Or should this be in another loop? As it doesn't seem to make sense that we want to overwrite this value with vtec_pronosticado on each iteration of this inner loop.
DINI
DINI on 1 Jul 2019
Sorry, Here you have all the code with the data. The initial cell array (result.mat) is the datos.mat that it is attached here
clear all
%cargo todas los puntos grilla
puntos_grilla = 4;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%CONDICIONES INICIALES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dia_inicial_pronostico = 251;
cantidad_de_iteraciones = 10;
dias_previos = 10;
intervalo = 60;
np = 2;
valores_por_dia = 24*60/intervalo;
npasoh = 60/intervalo;
%
step = 1/npasoh;
npaso = intervalo/15;
%
ivtec = valores_por_dia*(dia_inicial_pronostico-1); % punto anterior al comienzo de la prediccion
diaini = dia_inicial_pronostico-dias_previos; % dia inicial para hacer el entrenamiento/validacion/test de la NN
ndiaini = valores_por_dia*(diaini-1); % punto/ubicacion justo previo la implementacion NN
delta = ndiaini+dias_previos*24-1;
predecir = np*valores_por_dia;
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TRANSFORMO EL VTEC - LOG
%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:puntos_grilla
vtec_input = result{i};
vtec_intervalo = vtec_input(1:npaso:end,6);
time_intervalo = vtec_input(1:npaso:end,[2 5]);
vtec_log = log(vtec_intervalo(2:end,1)./vtec_intervalo(1:end-1,1));
vtec_logaritmico{1,i} = vtec_log;
%vtec_logaritmico es un cell-array donde cada celda corresponde a un
%punto grilla
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BUCLE PRONOSTICO
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j = 1: predecir
for i=1:puntos_grilla
for itera=1:cantidad_de_iteraciones
variable = vtec_logaritmico{i};
T = tonndata(variable(ndiaini+j:delta-1+j,1),false,false);
trainFcn = 'trainlm';
feedbackDelays = 1:3;
hiddenLayerSize = 10;
net = narnet(feedbackDelays,hiddenLayerSize,'open',trainFcn);
[x,xi,ai,t] = preparets(net,{},{},T);
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,x,t,xi,ai);
% Test the Network
y = net(x,xi,ai);
e = gsubtract(t,y);
performance = perform(net,t,y)
nets = removedelay(net);
nets.name = [net.name ' - Predict One Step Ahead'];
%view(nets)
[xs,xis,ais,ts] = preparets(nets,{},{},T);
ys = nets(xs,xis,ais);
stepAheadPerformance = perform(nets,ts,ys)
yys=cell2mat(ys);
tt=cell2mat(ts);
vtec_pronosticado(itera,j)= exp(yys(end))*vtec_intervalo(ivtec+j-1,1);
vtec_pronosticado2 = vtec_pronosticado';
vtec_final{1,i} = vtec_pronosticado2 ; %Cell-Array con n cantidad de columnas como n iteraciones tengamos
end
end
end
The problem is with the following part (the last one in the previous code).
vtec_final{1,i} = vtec_pronosticado2 ;

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 1 Jul 2019
As Geoff said, at the moment this line
vtec_final{1,i} = vtec_pronosticado2 ;
doesn't make much sense. We have:
for i=1:puntos_grilla
for itera=1:cantidad_de_iteraciones
%...
vtec_pronosticado2 = ...
vtec_final{1,i} = vtec_pronosticado2;
end
end
When i=1, itera=1, you calculate vtec_pronosticado2 and put that into vtec_final{1, 1}
Then i=1, itera = 2, you calculate another vtec_pronosticado2 but put that in the same vtec_final{1, 1}, overwriting the value you've written when itera was 1.
And so on, for i=1, itera=3:cantidad_de_iteraciones. In effect, the only value that matters is the last iteration of itera when it's equal to cantidad_de_iteraciones. The above could be rewritten to
for i=1:puntos_grilla
itera = cantidad_de_iteraciones; %only the last iteration matters
%...
% vtec_pronosticado2 = ...
vtec_final{1,i} = vtec_pronosticado2;
end
without changing the behaviour. But I doubt that's what you wanted. Problably, itera should be part of the indexing of vtec_final.
Also. vtec_final should be predeclared instead of growing it in a loop. Knowing the size that it's supposed to finish with would also hint at what is wrong with the above.
  1 Comment
DINI
DINI on 1 Jul 2019
Thanks for your answer, but, really I dont want to overwrite, actually it is precisely what I want to avoid. Because the first 4 laps makes them perfect well. But something happens that when he goes to write again in cell 1, he rewrites what was already written and in cell 1 he writes a copy of what is in the 4, when he goes to the 2nd round of the 2 he also rewrites in it the cell 4
AN EXAMPLE:
I WANT THIS:
%CELL 1x1
18,55365861 17,96233043 17,70956434 19,55243764 18,28604199 18,7480012 19,02871666 18,22729109 18,73414464 19,70369912
28,49074228 26,34053728 30,88022 24,04140136 27,28284062 25,82939223 23,8193441 26,36868726 25,38376867 26,70119335
21,91591015 22,12765369 23,25486488 21,05621051 21,5610289 22,40778888 20,77666976 20,60320358 19,00689335 22,78638869
14,21283555 16,29195304 14,23553182 16,26452148 15,14883689 14,56192959 14,06839482 15,94028603 15,68523002 15,59777422
11,31256346 13,03036833 12,0441074 11,29144324 9,78339633 11,67680221 12,3906915 11,72188605 11,49194882 9,68936799
13,26042164 14,23086853 15,98384564 13,58045932 14,16076448 13,89637351 10,33724927 12,06174781 15,23326745 16,36339458
9,787374444 8,4258252 8,507157363 11,10991755 9,902336665 9,342270415 9,983148695 8,987585411 8,427593685 11,06580966
6,549336883 7,863466803 7,112652908 6,960230572 6,954016805 7,454604353 6,676873434 6,67100292 6,67131859 8,381266873
5,680346594 5,694445266 5,173844814 4,288059679 4,041730754 5,027705779 5,114617136 5,078590842 4,532491302 3,698039032
5,353781654 3,63092853 3,54157609 7,538250358 3,419818527 4,593293207 4,932445013 3,820479016 4,955082133 4,376750225
4,337714905 3,675408367 4,082813535 3,286335844 3,748897098 4,347159122 4,270831063 4,041873428 4,009303576 4,040479741
3,82752784 5,024915611 3,562177572 3,558217166 3,181041931 3,621105405 3,802843067 4,212086704 3,995676658 3,576173165
%CELL 1x2
7,761020269 7,884850999 7,03741576 7,542581705 6,182814279 8,017593882 8,616572539 6,313515913 8,257207432 7,689173022
7,693880507 7,530944723 6,702508719 6,922210465 7,581743602 7,18465678 6,132456285 7,878406862 6,729993942 8,586249503
11,07163664 13,07303009 12,30638388 11,72955145 11,02651293 13,05037848 13,02906064 11,71965121 11,19503095 11,39357243
8,19280411 8,057490828 7,734163334 7,253220754 8,35011355 8,272505376 10,26081181 8,85178201 8,735065808 7,471689302
6,968354962 6,895041249 9,181937756 6,777048631 5,86853852 6,788411303 7,424078639 6,640172498 6,321688699 8,435061501
5,64767933 5,660277612 6,253247859 5,714912168 5,694718151 5,881735571 5,484756537 5,576074226 5,808388639 5,724607995
4,792168816 4,567052958 5,079473368 4,855524049 4,773587071 4,856290378 5,11393711 4,729459563 4,77213263 4,456833409
3,185135745 2,98429935 3,247339726 3,527432597 3,37885721 2,905049612 3,11026998 3,227583663 3,046719312 2,705059827
3,059863327 2,569506988 2,982606029 3,212603709 2,974767534 2,67340837 2,888054521 3,319074936 3,105102908 2,971157587
2,33969703 2,55412044 2,407985638 2,394008159 2,420555743 2,580213217 2,304953254 2,63261042 2,543072785 3,077156618
3,997049721 4,067252681 3,890484831 4,095710636 4,148938598 3,59565872 4,468017985 3,986422638 3,869730535 3,828487601
7,504112283 7,195706018 7,582893885 7,018883949 6,870115728 6,701213559 7,495733956 6,952235508 8,148568352 8,303949728
%CELL 1x3
9,7906344 9,757128129 9,874842138 10,13361332 10,79852593 9,764168118 10,22973274 10,20262916 10,32769248 9,917609003
6,284363724 5,423611975 6,28348028 7,962937394 6,354604013 6,361196566 6,237256145 6,097951724 6,618383604 6,453848624
4,274363205 3,883979264 4,446670275 3,778486671 4,716411681 4,34259398 4,853091832 3,934077806 4,785062743 5,646581116
7,242670351 4,706493858 6,484259773 5,111035144 5,277837952 6,021486379 5,425347358 4,009209428 5,31254217 6,400470481
5,781272658 6,018525064 6,087544843 6,052470322 6,075553498 6,163195511 5,998903267 6,099371081 6,397844378 5,730250441
5,68751714 6,126522971 5,859665734 6,308888661 5,986396963 6,109428582 5,904724581 6,239105041 6,040277752 6,145659112
5,507373566 5,302952243 5,399840124 5,368825299 5,50891407 5,381998085 5,510482789 5,474605398 5,347444485 5,110474115
3,911553459 3,789779358 3,553211802 3,56389023 3,787795909 3,542841683 3,949519256 3,876790368 3,52499778 3,832401204
7,656061556 5,298284803 7,118836555 7,612041047 7,013495187 6,40657608 8,021504991 6,64708996 7,363071736 6,996054798
5,573867568 6,306054323 6,9963745 7,799358636 6,625483756 5,43137616 3,912975353 6,560814135 7,207648369 6,078392092
4,524520148 5,546774796 5,016513163 4,409018697 4,859310628 4,284158812 4,605172129 4,330916629 5,954179434 4,715712396
4,927135478 4,774118515 4,944641867 4,868787526 4,868634015 4,848451074 4,931347521 5,077846902 4,76412873 4,901055063
%CELL 1x4
7,718163962 7,377422164 8,363357679 5,384795696 6,549898127 8,55339683 7,750224284 6,145283959 7,243614659 7,109673948
3,418864869 4,850442333 2,573393556 1,729407036 3,713047215 1,603847494 3,27656837 3,115140424 2,285450071 0,976987423
8,191062236 11,97429532 5,30852092 8,304369966 4,388324222 6,388075686 12,42319019 4,116371294 5,643036981 3,598520165
5,385127412 6,794761774 4,310002307 5,382440149 7,444358812 6,694679098 5,257472489 4,621798094 5,729500521 5,622849299
4,139354509 3,809269028 4,277999789 4,000962991 4,111364299 3,943349731 4,273595542 4,490641598 4,398746331 4,169026285
5,977412609 6,382313115 5,321391711 6,369614582 6,630048191 5,507424746 5,618869261 5,171433273 5,87870352 6,21761223
4,773862746 5,059160484 4,800817345 4,863171117 4,929597742 4,710219104 4,717681982 5,034931967 4,89473479 4,78609535
3,624232738 3,847079365 3,772046066 3,712632769 4,02736919 3,845808422 3,880296487 3,975375645 3,893846766 3,848349942
3,848020467 3,739859078 3,506307547 3,633934943 3,978937957 3,618795017 3,673549688 4,005444202 3,802230413 3,804862112
2,987357643 3,153647439 3,155035911 3,294690042 3,108398789 3,269458997 3,102920841 3,22472747 3,000624914 3,039742488
4,854870391 4,297573337 4,536775889 4,761015752 4,699686585 4,218456563 4,55404741 4,424905044 4,610787943 4,546353536
3,87413052 3,992281173 3,9761343 3,645619535 4,027908455 3,847492236 3,9471817 4,011329081 3,864788462 3,79566726
BUT, IN INSTEAD OF THAT, THE RESULTS ARE:
%CELL 1x1
7,06393573181532 7,63121399483052 8,94097994575406 5,49667130522573 6,69613637070022 6,85814775664212 6,87721307165438 6,69428358129336 8,07436109979654 6,34695214509061
1,50517747179360 1,72922189107669 1,85321853659344 0,661852179565949 8,70696968179222 3,12086202736136 12,6629001582839 3,68277455061190 1,35909444849677 3,18207586846193
7,62963568950150 5,32400146786149 7,03232009571863 12,2110794481648 6,62605871915195 7,06121073076556 5,23119104805581 7,53597989340182 5,22683247816248 6,69852670103164
4,40471901267283 5,93156335470317 5,27962220621578 5,90684983447766 5,40718951521254 6,51572504686296 6,56607541425826 5,84110696812424 7,79200838459842 6,35703055898295
4,31757373950905 4,43110852494111 3,79553056576093 4,10324304137888 3,90570304699416 4,15840058554335 4,39055130991068 4,42959577024792 4,10266029880526 4,21964942040777
7,60119763205773 5,31349270018929 5,43458709544635 6,35299304146832 6,14605668431751 5,94158365875114 6,36561107810151 5,61508951506669 5,72086049415967 6,05756102452084
5,37643534281677 4,91454118510662 5,02309269766780 4,88868141391996 5,12432288489652 4,60682454935833 5,11367635962503 4,88570639629051 4,64736782554911 5,34843312183856
4,18613584140463 3,79627281248633 3,94436174997041 3,94822688299667 3,75228017077684 4,15057929049468 4,02262908604200 4,15103800914974 3,85893652526191 3,97716382723262
3,73792388067801 3,24184884992190 3,79041705671610 3,54272265932771 3,60562581500180 3,75148476102062 3,77747949264537 3,45807370237129 3,94912991866966 3,75331837426789
3,08887838425918 3,21940829994532 3,17350130042741 3,05364713015875 3,15952317162268 3,18182885859360 3,27962479930391 3,23501067550779 3,16098771393315 3,25934596446161
4,66680124689887 4,75914804106159 4,88070340209568 4,61885717403941 4,60006433347238 4,59938902410139 4,62595179693131 4,71861287454156 4,81406304655513 4,04654940864919
3,34825452704544 3,77286553642882 4,11825714100129 3,57789914055171 3,69770902581224 3,32800677685148 4,51718106108128 4,12247453617280 3,95621291581306 3,55410743547326
%CELL 1x2
7,06393573181532 7,63121399483052 8,94097994575406 5,49667130522573 6,69613637070022 6,85814775664212 6,87721307165438 6,69428358129336 8,07436109979654 6,34695214509061
1,50517747179360 1,72922189107669 1,85321853659344 0,661852179565949 8,70696968179222 3,12086202736136 12,6629001582839 3,68277455061190 1,35909444849677 3,18207586846193
7,62963568950150 5,32400146786149 7,03232009571863 12,2110794481648 6,62605871915195 7,06121073076556 5,23119104805581 7,53597989340182 5,22683247816248 6,69852670103164
4,40471901267283 5,93156335470317 5,27962220621578 5,90684983447766 5,40718951521254 6,51572504686296 6,56607541425826 5,84110696812424 7,79200838459842 6,35703055898295
4,31757373950905 4,43110852494111 3,79553056576093 4,10324304137888 3,90570304699416 4,15840058554335 4,39055130991068 4,42959577024792 4,10266029880526 4,21964942040777
7,60119763205773 5,31349270018929 5,43458709544635 6,35299304146832 6,14605668431751 5,94158365875114 6,36561107810151 5,61508951506669 5,72086049415967 6,05756102452084
5,37643534281677 4,91454118510662 5,02309269766780 4,88868141391996 5,12432288489652 4,60682454935833 5,11367635962503 4,88570639629051 4,64736782554911 5,34843312183856
4,18613584140463 3,79627281248633 3,94436174997041 3,94822688299667 3,75228017077684 4,15057929049468 4,02262908604200 4,15103800914974 3,85893652526191 3,97716382723262
3,73792388067801 3,24184884992190 3,79041705671610 3,54272265932771 3,60562581500180 3,75148476102062 3,77747949264537 3,45807370237129 3,94912991866966 3,75331837426789
3,08887838425918 3,21940829994532 3,17350130042741 3,05364713015875 3,15952317162268 3,18182885859360 3,27962479930391 3,23501067550779 3,16098771393315 3,25934596446161
4,66680124689887 4,75914804106159 4,88070340209568 4,61885717403941 4,60006433347238 4,59938902410139 4,62595179693131 4,71861287454156 4,81406304655513 4,04654940864919
5,26405690950963 5,15019495804716 5,30746273579584 5,25220952403681 5,37076250209689 5,18849543559181 5,04721005633310 5,85203276068411 4,81218330097898 4,42166408635541
%CELL 1x3
7,06393573181532 7,63121399483052 8,94097994575406 5,49667130522573 6,69613637070022 6,85814775664212 6,87721307165438 6,69428358129336 8,07436109979654 6,34695214509061
1,50517747179360 1,72922189107669 1,85321853659344 0,661852179565949 8,70696968179222 3,12086202736136 12,6629001582839 3,68277455061190 1,35909444849677 3,18207586846193
7,62963568950150 5,32400146786149 7,03232009571863 12,2110794481648 6,62605871915195 7,06121073076556 5,23119104805581 7,53597989340182 5,22683247816248 6,69852670103164
4,40471901267283 5,93156335470317 5,27962220621578 5,90684983447766 5,40718951521254 6,51572504686296 6,56607541425826 5,84110696812424 7,79200838459842 6,35703055898295
4,31757373950905 4,43110852494111 3,79553056576093 4,10324304137888 3,90570304699416 4,15840058554335 4,39055130991068 4,42959577024792 4,10266029880526 4,21964942040777
7,60119763205773 5,31349270018929 5,43458709544635 6,35299304146832 6,14605668431751 5,94158365875114 6,36561107810151 5,61508951506669 5,72086049415967 6,05756102452084
5,37643534281677 4,91454118510662 5,02309269766780 4,88868141391996 5,12432288489652 4,60682454935833 5,11367635962503 4,88570639629051 4,64736782554911 5,34843312183856
4,18613584140463 3,79627281248633 3,94436174997041 3,94822688299667 3,75228017077684 4,15057929049468 4,02262908604200 4,15103800914974 3,85893652526191 3,97716382723262
3,73792388067801 3,24184884992190 3,79041705671610 3,54272265932771 3,60562581500180 3,75148476102062 3,77747949264537 3,45807370237129 3,94912991866966 3,75331837426789
3,08887838425918 3,21940829994532 3,17350130042741 3,05364713015875 3,15952317162268 3,18182885859360 3,27962479930391 3,23501067550779 3,16098771393315 3,25934596446161
4,66680124689887 4,75914804106159 4,88070340209568 4,61885717403941 4,60006433347238 4,59938902410139 4,62595179693131 4,71861287454156 4,81406304655513 4,04654940864919
3,97232535034711 4,05834293252571 3,97090487624332 4,03183640659037 4,02355313042865 4,00013675858823 4,13828597349920 4,50677494163783 3,98635199349018 4,10558889891757
%CELL 1x4
7,06393573181532 7,63121399483052 8,94097994575406 5,49667130522573 6,69613637070022 6,85814775664212 6,87721307165438 6,69428358129336 8,07436109979654 6,34695214509061
1,50517747179360 1,72922189107669 1,85321853659344 0,661852179565949 8,70696968179222 3,12086202736136 12,6629001582839 3,68277455061190 1,35909444849677 3,18207586846193
7,62963568950150 5,32400146786149 7,03232009571863 12,2110794481648 6,62605871915195 7,06121073076556 5,23119104805581 7,53597989340182 5,22683247816248 6,69852670103164
4,40471901267283 5,93156335470317 5,27962220621578 5,90684983447766 5,40718951521254 6,51572504686296 6,56607541425826 5,84110696812424 7,79200838459842 6,35703055898295
4,31757373950905 4,43110852494111 3,79553056576093 4,10324304137888 3,90570304699416 4,15840058554335 4,39055130991068 4,42959577024792 4,10266029880526 4,21964942040777
7,60119763205773 5,31349270018929 5,43458709544635 6,35299304146832 6,14605668431751 5,94158365875114 6,36561107810151 5,61508951506669 5,72086049415967 6,05756102452084
5,37643534281677 4,91454118510662 5,02309269766780 4,88868141391996 5,12432288489652 4,60682454935833 5,11367635962503 4,88570639629051 4,64736782554911 5,34843312183856
4,18613584140463 3,79627281248633 3,94436174997041 3,94822688299667 3,75228017077684 4,15057929049468 4,02262908604200 4,15103800914974 3,85893652526191 3,97716382723262
3,73792388067801 3,24184884992190 3,79041705671610 3,54272265932771 3,60562581500180 3,75148476102062 3,77747949264537 3,45807370237129 3,94912991866966 3,75331837426789
3,08887838425918 3,21940829994532 3,17350130042741 3,05364713015875 3,15952317162268 3,18182885859360 3,27962479930391 3,23501067550779 3,16098771393315 3,25934596446161
4,66680124689887 4,75914804106159 4,88070340209568 4,61885717403941 4,60006433347238 4,59938902410139 4,62595179693131 4,71861287454156 4,81406304655513 4,04654940864919
4,03428953126304 3,81448665879785 4,10132725186935 3,92699519665798 3,96126532023131 3,70199824806817 3,96849095921914 3,88146178821412 3,79700754334704 4,09429549937918
IF YOY PAY ATTENTION, THE CELLS 1,2 AND 3 ARE OVERWRITTEN BY THE RESULTS IN 4

Sign in to comment.

Categories

Find more on Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!