How can we split data into sub arrays based on dates? Lets say we do have thousands data points for different dates (for a week) . I want to split this data into arrays based on the dates as the number of observations is changing from date to date. The following is a sample from my data. Many thanks in advance

 채택된 답변

Image Analyst
Image Analyst 2014년 12월 23일

1 개 추천

I'd extract the columns and use the unique() function on them
col1 = array(:, 1);
col2 = array(:, 2);
col3 = array(:, 3);
uniqueDates = unique(col1);
uniqueTimes = unique(col2);
Then use a for loop to find the index of the dates, and place the other two columns into a cell array. You need a cell array because you might possibly have different numbers of times for each date.
for k= 1 : length(uniqueDates)
thisDate = uniqueDates(k);
indexesWithThisDate = (col1 == thisDate);
% Extract all rows with this date and put into a cell array.
theseData = array(indexesWithThisDate, 2:3); % Get cols 2 and 3
% Assign to a new cell
ca{k} = theseData;
end
You might not need all of the lines in the first chunk of code. This code is untested, so be sure to use the debugger to step through it in the case of problems.

추가 답변 (2개)

dpb
dpb 2014년 12월 23일

2 개 추천

Convert to datenums and then make selections based on those...of course, convert the comparison dates to datenum as well.

댓글 수: 3

Bido12
Bido12 2014년 12월 23일
Thank you. I Know I need to convert it using datenum. But I don't know how to write a correct matlab code to divide them according to the date. I tried a lot but I have no result.
Image Analyst
Image Analyst 2014년 12월 23일
Considering that you haven't even told us what form or format your dates are in, we don't know either. You say you've "tried a lot" but you didn't share any of those with us. What did you try? Can you provide any data? Have you read this yet?
Bido12
Bido12 2014년 12월 23일
편집: Bido12 2014년 12월 23일
I'm sorry for that. This is a sample of my data. The first column is MATLAB date and Time format. I need to divide my data according to date. My data is for seven days.

댓글을 달려면 로그인하십시오.

Guillaume
Guillaume 2014년 12월 23일

1 개 추천

Most likely, you want to use the third return value unique on datenums or datevecs to find out how to split your data, and possibly accumarray to perform operations on the other columns.
For example:
data = [2014 12 19 1
2014 12 19 1.2
2014 12 20 2.2
2014 12 19 1.4
2014 12 20 2
2014 12 20 2.6
2014 12 20 2.4
2014 12 21 3.5]; %year month day value
[dates, ~, dateidx] = unique(data(:, 1:3), 'rows'); %find out where the dates go
dataperday = arrayfun(@(idx) data(dateidx==idx, 4), 1:size(dates, 1), 'UniformOutput', false)'; %split into cell arrays
averageperday = accumarray(rows, data(:, 4), [], @mean); %calculate mean per day
See also this answer

카테고리

도움말 센터File Exchange에서 Time Series Objects에 대해 자세히 알아보기

질문:

2014년 12월 23일

댓글:

2014년 12월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by