Info
此问题已关闭。 请重新打开它进行编辑或回答。
Saving values from a loop but allocating them to a different index of a vector based on a different value?
1 次查看(过去 30 天)
显示 更早的评论
Howdy again,
So here is what I'm trying to do. I have a datetime array (format = 'yyyy/MM/dd h:mm:ss a'). I want to obtain the number of counts of events that occur per hour per day as well as per minute per hour per day. But, I want to set up two zeros arrays (one for minutes (zeros(1440,1)) and one for hours (zeros(24,1))) and I want the index of those to represent in the following way:
H = zeros(24,1);
H(1) = counts of events occurring between 00:00:00 and 00:59:00
H(2) = counts of events occurring between 01:00:00 and 01:59:00
.
.
.
H(24) = counts of events occurring between 23:00:00 and 23:59:00
for each new day.
And,
M = zeros(1440,1);
M(1) = counts of events occurring between 00:00:00 and 00:00:59
M(2) = counts of events occurring between 00:01:00 and 00:01:59
.
.
.
M(1440) = counts of events occurring between 23:59:00 and 23:59:59
So the problem is, the dates coming in have different start dates and start times (they are random). So, for sample 1, I might have '2016/05/23 10:46:30 AM', but for sample 2 I might have '2015/12/01 3:12:35 PM'. The datetimes per individual sample are incrementing, and each sample will be run through independently.
I want to store what I'm obtaining into the right index in H and M so that it's representative of how many events occurred across all years, months, and days, in hour 5 of the day or minute 394 of the day or so forth.
This is what I have: (E is a cell array containing the dates and times, G is the datetime array)
% Get counts per minute per day by DOW
countMinE = zeros(1440,1); % 1440 minutes in a day. Let index 1 = time
% from 00:00:00 until 00:00:59, index 2 = time from 00:01:00 until 00:01:59...
countHourE = zeros(24,1); % run for count per hour too
countMinE(1) = 1;
countHourE(1) = 1;
countM = 1;
countH = 1;
for i = 1:length(E)-1
[~,~,d] = ymd(G);
[h,m,~] = hms(G);
countM = 1;
countH = 1;
if d(i+1) == d(i)
% same day so check next hour then next minute
if h(i+1) == h(i)
countH = countH+1;
countHourE(i+1) = countH; %%%%This is where I want the value to get stored to the index that the hour is (so if the hour was 13, I'd want a count added to index 13)
% same hour, check same minute
if m(i+1) == m(i) % another count in same minute, add count
countM = countM+1;
countMinE(i+1) = countM; %%%%This is where I want the value to get stored to the index that the minute is (so if the minute was 45 and the hour was 3, I'd want a count added to index minutes(hours(3.75)) = 225)
else % new minute
% do nothing
end
else % new hour
% do nothing
end
else % new day
% do nothing
end
end
Can anyone help me figure this one out? It's a tough nut to crack for me...
Thanks!
0 个评论
回答(0 个)
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!