How to subsample a dataset per week
2 views (last 30 days)
Show older comments
Hi,
I would like to subsample a dataset in such a way as to keep only the data that was collected on the first sampled day of the week (I mean the first sampled day of the week here, not the first of the week). The desired out is desired_output.
How would I do that ?
Thank you,
j = [1 2 3 4 5 6 7 8 9]';
i = datetime({'2021-01-01', '2021-01-02', '2021-01-02' , '2021-01-04', '2021-01-04', '2021-01-08', '2021-01-09', '2021-01-15', '2021-01-21'})';
w = week(i)
t = table(j, i, w)
[a, b, c] = unique(t(:, [2:3]), 'rows');
desired_output = t([1,4:5,8:9],:)
0 Comments
Answers (1)
Shashank Gupta
on 1 Feb 2021
Hi,
I am assuming you have the table t and then you want to know first sampled data of the week. As far as I know there is no function to directly solve this, you might need to apply a brute force way, I have a reference script for you. it is unoptimised but you can refer and create one for your use case.
% Assuming we have table t with us. All the below processing done by considering we have t.
% idx will store all the first data sampled in each week.
idx = [];
uni = unique(t.w);
tt = 1;
for i=1:length(t.w)
if(uni(tt)==t.w(i))
idx = [idx i];
tt=tt+1;
end
end
% final_idx array will have the index which you required.
% final_idx is one step forward to idx array, it takes up
% all the data which can be sampled from the first day of the week.
final_idx = [];
for i=1:length(idx)
final_idx = horzcat(final_idx,find(t.i==t.i(idx(i)))');
end
% desired output.
desired_output = t(final_idx,:);
I hope this helps.
Cheers.
3 Comments
See Also
Categories
Find more on Data Type Identification 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!