Splitting a large column into other columns
显示 更早的评论
Hello,
I have a huge DateTime column vector that gives me values of a parameter every ten minutes, however some intervals had to be deleted because they were giving false measurements. So, while some days maybe represented with 144 rows (1440 minutes = 24 hours) others might be represented with a smaller number of rows. I would like to split the values of the parameter so that each column represents a day. I would also like to have this day's number ie Jan would be 1 Feb 1 would be 32 and so on.
jour=NaN(144,31);
A=day(DateTimeVrai);
jour(1,1)=A(1,1);
for j=1:31
for k=2:length(A)
if A(k,1)=A(k-1,1)
jour(k,j)=A(k,1);
else
jour(k,j+1)=A(k,1);
end
end
I wrote the previous code to split the array but it didn't work.
Maybe histcount could create the bins by noticing when the value of A, (when the day) changes, but I don't know how to do it?
I especially don't know how to make Matlab count the days of the year as explained before (1 Jan = 1, 2 Jan = 2,...1 Feb = 32..., 31 Dec=365)
采纳的回答
更多回答(1 个)
Converting your datetime array to a number of days since a reference day is easy. Subtract the reference day from the datetime array, so you obtain a duration array. Convert the duration array to days using the days function. So:
jourreference = datetime(1, 1, DateTimeVrai(1, 1).Year); %1st January of whichever year is in your datetime array
jour = days(DateTimeVrai(:, 1) - jourreference);
For splitting your array into several columns, there is a problem, you haven't told us what you want to do with the shorter columns. A matrix obviously has to have the same number of rows for all columns. I'm assuming you want to fill the shorter columns with NaNs.
Histogram functions won't help. First thing to do, if the days numbers are not all consecutive and starting from 1, is to convert the jour array into an array going from 1 to the number of days. Use the third return value of unique for that. You can then feed that to accumarray (or use an explicit for loop) to build a cell array of columns. Finally expand the shorter columns and concatenate it all into a matrix. So:
[~, ~, jourindex] = unique(jour);
colonnesjour = accumarray(jourindex, DateTimeVrai(:, 2), [], @(c) {c(:)}); %group into columns of days
maxhauteur = max(cellfun(@numel, colonnesjour)); %find height of largest column
colonnesjour = cellfun(@(c) [c; nan(maxhauteur - numel(c), 1)], colonnesjour, 'UniformOutput', false); %add nan to shorter columns
colonnesjour = [colonnesjour{:}]; %and convert to matrix.
Note that if you hadn't deleted the invalid rows but just replaced them with nans, then you would not have to worry about resizing the column (which basically reverse your delete operation).
7 个评论
Eliot Tabet
2016-2-9
Guillaume
2016-2-9
Well, if you want 'Jan 1 = 1, etc.' then jourreference is the 1st January of the year. You can of course, use any date as a reference. It does not change anything to the rest of the code.
I'd made a slight mistake on the last line, but there should be no reason for you to get an 'index exceeds matrix dimensions' error on any of the lines. The only possible causes are:
- DateTimeVrai does not have 2 columns (where is the data then?)
- you've shadowed the built-in max or nan with a variable of the same name.
You can attach your data to a comment if you want.
Eliot Tabet
2016-2-9
Guillaume
2016-2-9
Eliot's comment moved here from Answer:
[~, ~, jourindex] = unique(jour);
colonnesjour = accumarray(jourindex, DateTimeVrai(:, 1), [], @(c) {c(:)}); %group into columns of days
maxhauteur = max(cellfun(@numel, colonnesjour)); %find height of largest column
colonnesjour = cellfun(@(c) [c; nan(maxhauteur - numel(c), 1)], colonnesjour, 'UniformOutput', false); %add nan to shorter columns
colonnesjour = [colonnesjour{:}]; %and convert to matrix.
and now the error is:
Undefined function 'accumarray' for input arguments of type 'datetime'.
Error in Untitled3 (line 13)
colonnesjour = accumarray(jourindex, DateTimeVrai(:, 1), [], @(c) {c(:)}); %group into columns of days
Guillaume
2016-2-9
Eliot's comment moved here from Answer:
jourreference='01/01';
jourreference=datetime(jourreference,'InputFormat','dd/MM');
jourreference.Year=year(DateTimeVrai(1));
njour=DateTimeVrai(:,1)-jourreference;
njour=days(njour);
njour=njour+1;
njour=fix(njour);
This is the code I ran to put an index for each day, so now I have 144 rows of value 50, this is every 10 mins of 19/02/2010 and 144 51 and so on. Now I want to split them, but how !?
Guillaume
2016-2-9
Please use comments rather than starting a new answer. It's hard to follow otherwise.
For your first comment. Since you were using 2d referencing in your question, I assumed that DateTimeVrai had two columns, the first being the datetime, the second the corresponding values. There is no point in writing DateTimeVrai(x, 1) if it only has one column. DateTime(x) works just as well.
The second argument to accumarray should be the data, whatever that is. It certainly should not be a datetime array.
Here is an example:
%build some demo data:
DateTimeVrai = repelem(datetime(2015,1,1):datetime(2015,3,6), 50); %column of datetimes repeated 50 times
donnees = [1:numel(DateTimeVrai )]';
aenlever = randperm(numel(DateTimeVrai ), 300); %rows to remove at random
DateTimeVrai(aenlever) = [];
donnees(aenlever) = [];
%process data:
jourreference = [1 1]; %month day
reference = datetime([DateTimeVrai(1).Year jourreference]);
njour = fix(days(DateTimeVrai - reference) + 1);
[~, ~, jourindex] = unique(njour);
colonnesjour = accumarray(jourindex, donnees, [], @(c) {c(:)});
maxhauteur = max(cellfun(@numel, colonnesjour));
colonnesjour = cellfun(@(c) [c; nan(maxhauteur - numel(c), 1)], colonnesjour, 'UniformOutput', false);
colonnesjour = [colonnesjour{:}];
Hello,
I have a text file with three columns and 5.000.000 data (row), I need to split it into the following shape:
a1 b1 c1 a2001 b2001 c2001 a4001 b4001 c4001 ...
... ... ... ... ... ... ... ... ...
a2000 b2000 c2000 a4000 b4000 c4000 a6000 b6000 C6000 ...
Would you please let me know how to manage that?
类别
在 帮助中心 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!