- sample = 4x192
- Weekend = 4x72
- Weekday = 4x120
Separating Matrix by Column By Pattern?
1 次查看(过去 30 天)
显示 更早的评论
Hi all,
I have count data based in time. I have a matrix that is essentially counts of an event occurring within 96 15 minute intervals per day over some 300 days. It's shaped like this:
sample = [0, 0 , 1, 5, 0, 0, 2, 3, 2, 0, 1, 4, 0, 0, 1, 2, 5, 0, 0, 0, 1, 1, 0, 1;
1, 0 , 2, 1, 2, 0, 4, 1, 2, 0, 1, 2, 0, 0, 1, 2, 5, 0, 0, 0, 2, 3, 0, 1;
0, 0 , 4, 4, 1, 0, 3, 3, 1, 1, 2, 1, 0, 0, 1, 2, 1, 0, 1, 2, 3, 4, 1, 3;
2, 0 , 1, 5, 0, 0, 2, 1, 2, 0, 1, 4, 0, 0, 1, 2, 5, 0, 0, 0, 1, 1, 0, 2];
% the columns refer to the hour, here 24 hours (so one day)
% the rows refer to the counts of events per 15 minute interval (4 per hour)
My problem is that I need to separate this matrix out into weekdays and weekends. So, I know that I start on a Sunday in my data (but I'd like some kind of if-else statement to check that condition, I do have the day-of-week data stored). Basically, I need to find a way to write a loop to allocate specific columns into a matrix Weekday or Weekend based on if the day the column represents is a weekday or weekend.
So I was thinking of something where, for the case where the data begins on a Sunday, I separate the matrix by taking the first 24 columns and putting them in Weekend, taking the next 120 columns and putting them in Weekday, the next 48 columns to Weekend, and so on. But I'm having an issue writing the code to do this in a nice and fast way. I also want to handle cases when the data doesn't start on Sunday (like, if it was a Monday first, then the pattern would be 120 columns to Weekday, 48 to Weekend, and so on).
% So I want something like this: Assuming the first 24 columns were Sunday:
sample = [0, 0 , 1, 5, 0, 0, 2, 3, 2, 0, 1, 4, 0, 0, 1, 2, 5, 0, 0, 0, 1, 1, 0, 1, 0, 2, 3, 2, 0, 1, 4, 0, 0, 1, 2, 5, 0, 0, 0 , 1, 5, 0, 0, 2, 3, 2, 0, 1;
1, 0 , 2, 1, 2, 0, 4, 1, 2, 0, 1, 2, 0, 0, 1, 2, 5, 0, 0, 0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starting on a Sunday. Are these the correct sizes of your input/output matrices:
采纳的回答
Guillaume
2018-10-10
Sounds very easy:
starttime = datetime(2018, 9, 10); %whatever your start is
sampletime = starttime + hours(0:size(sample, 2)-1);
issampleweekend = isweekend(sampletime);
sampleweekday = sample(:, ~issampleweekend);
sampleweekend = sample(:, issampleweekend);
A loop is certainly not needed.
更多回答(1 个)
the cyclist
2018-10-10
This will do what you want assuming that the data start on Sunday.
NDAYS = 300;
HOURS_PER_DAY = 24;
DAYS_PER_WEEK = 7;
% Generate some random input data
sample = rand(4,HOURS_PER_DAY*NDAYS);
% Reshape the input data so that each day is a slice in the 3rd dimension
sampleByDay = reshape(sample,4,HOURS_PER_DAY,[]);
% Find indices to Saturday and Sunday, and combine them
sundayIndex = 1:DAYS_PER_WEEK:NDAYS;
saturdayIndex = 7:DAYS_PER_WEEK:NDAYS;
weekendIndex = sort([sundayIndex saturdayIndex]);
% All non-weekend indices are weekday indices
weekdayIndex = setxor(1:NDAYS,weekendIndex)
% Use the indices to separate the weekend and weekday data
weekendByDay = sampleByDay(:,:,weekendIndex);
weekdayByDay = sampleByDay(:,:,weekdayIndex);
% Reshape back to two-dimensional
weekend = reshape(weekendByDay,4,HOURS_PER_DAY*size(weekendByDay,3));
weekday = reshape(weekdayByDay,4,HOURS_PER_DAY*size(weekdayByDay,3));
You will need to do something more clever in defining weekendIndex and weekdayIndex for the more general case.
The heart of the solution, reshaping such that each day is its own slice in the 3rd dimension, will remain the same.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!