How to split numeric values and store them?

I have the array below with activities 1, 2 and 3 and the times the system has spent in these activities. Not every row is a new date. For example, the first value (24) is 1/1/2015, however, the two following values (22.3 + 1.7 = 24) is on 1/2/2015, and so on.
Modes2 =
0 0 24.0000
0 0 22.3000
0 0 1.7000
0 0 13.0000
0 11.0000 0
0 24.0000 0
0 24.0000 0
0 24.0000 0
0 24.0000 0
0 17.2833 0
0 3.7667 0
2.9500 0 0
7.1167 0 0
16.8833 0 0
24.0000 0 0
To further analyse these, the need to be stored per 6 hours in something like the following.
Time_periods1 =
1/1/2015 6 hr 0 0 0
1/1/2015 12 hr 0 0 0
1/1/2015 18 hr 0 0 0
1/1/2015 24 hr 0 0 0
1/2/2015 6 hr 0 0 0
1/2/2015 12 hr 0 0 0
1/2/2015 18 hr 0 0 0
1/2/2015 24 hr 0 0 0
1/3/2015 6 hr 0 0 0
1/3/2015 12 hr 0 0 0
1/3/2015 18 hr 0 0 0
1/3/2015 24 hr 0 0 0
1/4/2015 6 hr 0 0 0
1/4/2015 12 hr 0 0 0
I wrote the following, does not work, now I'm lost.
for i=1:size(Modes2,1)
for j=1:size(Time_periods2,1)
for k=1:7
if (Modes2(i,k)>0 && Modes2(i,k)<=6)
Time_periods2(j,k)=Modes2(i,k);
elseif (Modes2(i,k)>6 && Modes2(i,k)<=12)
Time_periods2(j+1,k)=Modes2(i,k)-6;
Time_periods2(j,k)=6;
elseif (Modes2(i,k)>12 && Modes2(i,k)<=18)
Time_periods2(j+2,k)=Modes2(i,k)-12;
Time_periods2(j+1,k)=6;
Time_periods2(j,k)=6;I
elseif (Modes2(i,k)>18 && Modes2(i,k)<=24)
Time_periods2(j+3,k)=Modes2(i,k)-18;
Time_periods2(j+2,k)=6;
Time_periods2(j+1,k)=6;
Time_periods2(j,k)=6;
end
end
end
end
Any ideas?

3 件のコメント

Stephen23
Stephen23 2019 年 11 月 19 日
編集済み: Stephen23 2019 年 11 月 19 日
Showing the expected output helps us much more than showing non-functioning code.
Please show the complete expected output for the sample data you have given.
Note that not everyone on this forum is from the USA, and readers might interpret "1/2/2015" as the 1st of February 2015. Using ISO 8601 dates is unambiguous.
Rik
Rik 2019 年 11 月 19 日
It looks like the brunt of your code is trying to determine when 24 hours have passed. To find the sequential day, you could also use the line of code below.
day_ID=ceil(cumsum(sum(Modes2,2))/24);
If you use that to generate the first column, wouldn't that be fairly close to what you mean?
Ymkje Lize Neuteboom
Ymkje Lize Neuteboom 2019 年 11 月 19 日
This is the expected output for the sample.
New =
0 0 6.0000
0 0 6.0000
0 0 6.0000
0 0 6.0000
0 0 6.0000
0 0 6.0000
0 0 6.0000
0 0 6.0000
0 0 6.0000
0 0 6.0000
0 5.0000 1.0000
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
0 6.0000 0
2.9500 3.0500 0
6.0000 0 0
6.0000 0 0
6.0000 0 0
6.0000 0 0
6.0000 0 0
6.0000 0 0
6.0000 0 0
6.0000 0 0

サインインしてコメントする。

 採用された回答

Rik
Rik 2019 年 11 月 19 日

0 投票

The code below should do the trick. You will also notice there are much more comments explaining what the code is attempting to do.
Modes2 =[
0 0 24.0000
0 0 22.3000
0 0 1.7000
0 0 13.0000
0 11.0000 0
0 24.0000 0
0 24.0000 0
0 24.0000 0
0 24.0000 0
0 17.2833 0
0 3.7667 0
2.9500 0 0
7.1167 0 0
16.8833 0 0
24.0000 0 0];
len=6;%set the boundary so we can change it later if needed
in=Modes2;%make a copy so we can change the data
out=zeros(ceil(sum(in(:))/len),size(in,2));%pre-allocate the output
ind_out=1;%start at row 1
for ind_in=1:size(in,1)
while sum(in(ind_in,:))>0
ExtractAtMost=len-sum(out(ind_out,:));
tmp=min(ExtractAtMost,in(ind_in,:));
%this min() operation works element by element, so it relies on
%every row having only 1 non-zero element
%store to output
out(ind_out,:)=tmp+out(ind_out,:);
%reduce input to prepare for next iteration
in(ind_in,:)=in(ind_in,:)-tmp;
if len-sum(out(ind_out,:)) <= 2*eps
%test if output row is already full, if so, go to next row
ind_out=ind_out+1;
end
end
end

2 件のコメント

Ymkje Lize Neuteboom
Ymkje Lize Neuteboom 2019 年 11 月 19 日
Thank you so much, also for the clear explaination!
Rik
Rik 2019 年 11 月 19 日
You're welcome. If this solved your question, feel free to mark this as accepted answer. If not, feel free to comment with your remaining issues.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeHistorical Contests についてさらに検索

製品

リリース

R2019a

質問済み:

2019 年 11 月 19 日

コメント済み:

Rik
2019 年 11 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by