How to store and access values of multiple nested loops? - 4dimensional

조회 수: 1 (최근 30일)
Antonia Mauch
Antonia Mauch 2022년 5월 20일
답변: bransa 2022년 5월 20일
Hi guys,
this is my code:
day=140;
Name='winter';
data.battery.QQQ=zeros(10,24,140,number);
bat=[5 10 15 20 25 30 35 40 45 50];
for k=1:length(bat)
C=bat(k);
data.battery.SoC_int=1; %Start.
data.battery.Q=zeros(24*day+1,number); %kWh
data.battery.SoC=zeros(24*day+1,number); %0-1
data.battery.t=zeros(24*day+1,1); %0-1
data.battery.SoC(1,:)=data.battery.SoC_int;
data.battery.Q(1,:)=C(:,1)*data.battery.SoC_int;
data.battery.Check=ones(number,1);
data.PhotoEnergy.(Name)=zeros(24,number);
data.battery.CheckQ=zeros(10,24,140,number);
for i=1:day
for j=1:24
tempor=data.battery.Q((i-1)*24+j,:)+data.PhotoEnergy.(Name)(j,:)-data.ConsumptionEnergy.(Name)(j);
tempor(tempor<0)=0;
tempor(tempor>C(:,1))=C(:,1);
data.battery.Q((i-1)*24+j+1,:)=tempor;
data.battery.t((i-1)*24+j+1,1)=data.battery.t((i-1)*24+j,1)+1;
data.battery.SoC((i-1)*24+j+1,:)=data.battery.Q((i-1)*24+j+1,:)/C(:,1);
data.battery.QQQ(k,j,i,:)=data.battery.Q((i-1)*24+j+1,:);
end
end
end
I would like to store all of the results of data.battery.Q.
Before I added the third for-loop (for k=1:len...) the results got displayed like colums=number and rows=i*j (see white table in picture underneath).
The additional dimension is making it far more difficult.The generated k-loop values are overwritten with the subsequent k-loop values, so that I can only access the results of 'bat=50'.
I'd like to store the results like shown in the next figure:
Among other things, I tried with (as you can see in the code above):
data.battery.QQQ(k,j,i,:)=data.battery.Q((i-1)*24+j+1,:);
But here generated k-loop values are still overwritten with the subsequent k-loop values.
Even though no error occures, I think I made a mistake in the data.battery.QQQ code or something is missing.
A hint could be:
Can anyone help me how to store my generated data as whished?
Thanks a lot in advance,
Toni

답변 (1개)

bransa
bransa 2022년 5월 20일
Could you store the white tables (the results displayed like colums=number and rows=i*j) in a cell array instead? that is something like
data.battery.QQQ = cell(length(bat),1);
for k = 1:length(bat)
...
data.battery.QQQ{k}=data.battery.Q((i-1)*24+j+1,:);
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by