Reputation: 1637
I have a dataset with dates on 1 column and values on another column
'4/26/2016' 23
'4/26/2016' 25
'4/26/2016' 1
'4/27/2016' 10
'4/27/2016' 40
'4/27/2016' 67
'4/28/2016' 24
'4/28/2016' 13
'4/28/2016' 7
'4/28/2016' 99
How do I separate the values into different days? e.g., 4/27/2016 will have values of 10, 40 and 67 and so on. The dates might be different depending on the dataset that I am using so there is no fixed dates. Thanks!
Upvotes: 0
Views: 243
Reputation: 45752
We don't know what data structure you are using so we'll just have to assume it:
%% Your data
dates = {'4/26/2016';'4/26/2016';'4/26/2016';'4/27/2016';...
'4/27/2016';'4/27/2016';'4/28/2016';'4/28/2016';...
'4/28/2016';'4/28/2016'};
values = [23; 25; 1; 10; 40; 67; 24; 13; 7; 99];
[uniqueDates,~,sub] = unique(dates)
valuesPerDate = accumarray(sub, values, [], @(x){x})
%OPTIONAL
combined = [uniqueDates, valuesPerDate]
I would actually recommend first converting your dates into the new datetime
type, i.e. dates = datetime(dates)
and then later combined = [num2cell(uniqueDates), valuesPerDate]
, but if not then at least use a more logical format such as 'yyyy-mm-dd'
so that their alphabetical ordering is the same as their chronological ordering.
Upvotes: 2
Reputation: 259
%% Your data
date = {'4/26/2016';'4/26/2016';'4/26/2016';'4/27/2016';...
'4/27/2016';'4/27/2016';'4/28/2016';'4/28/2016';...
'4/28/2016';'4/28/2016'};
data = [23; 25; 1; 10; 40; 67; 24; 13; 7; 99];
%% First convert the date to datenum for ease
date = datenum(date,'mm/dd/yyyy');
%% Get the number of days
unique_days = unique(date);
%% Loop through the different days and extract corresponding data
%% - You might want to add unique identifiers to the variable names
for idx = 1:length(unique_days)
curr_day = unique_days(idx); % Get current date
idx_days = date == curr_day; % indices of data for current date
data_results{idx} = data(idx_days); % results
end
Upvotes: 2