Issue with data imputation, (inserting date time value into an array of dates).
Show older comments
Have been experimenting with data imputation and am running into an issue with rebuilding a datetime vector. Currently working with datasets upwards of 200k elements so attached at the bottom of this post is my test code using a smaller arbitrary data set. The error I'm getting is:
Error using datetime/horzcat (line 1341)
Dimensions of arrays being concatenated are not consistent, which is strange to me, because i've tested values to build a new datetime vector.
%%
t1 = (datetime('2020-06-30 23:45:00') : -minutes(15) : datetime('2020-06-28 00:00:00')).';
t1.Format = 'yyyy-MM-dd HH:mm:ss';
t2 = t1;
t2(2) = []; %forcing a missing value in order to create a loop to re-insert the now missing value.
%note: t1 is the reference vector with no missing values, t2 is the dataset with impurities
k = 0;
for i = 1:length(t2)
if t2(i) ~= t1(i)
counter = i;
k = k+1;
log(k) = i; %to maintain a log of every missing datetime value.
%need to insert missing element
temp(k) = t1(i);
t2 = [t2(1:i-1), temp(k), t2(i:end)];
end
end
***My test to understand what is happening is as follows:
t2(1:i-1)
ans =
datetime
2020-06-30 23:45:00 %as expected
temp(k)
ans =
datetime
2020-06-30 23:30:00 %as expected
newtimevec = [t2(1:i-1),temp(k)]
newtimevec =
1×2 datetime array
2020-06-30 23:45:00 2020-06-30 23:30:00 %as intended
newtimevec = [t2(1:i-1), temp(k), t2(i:end)]
% Error using datetime/horzcat (line 1341)
% Dimensions of arrays being concatenated are not consistent.
So from the above, I see that concatenating datetimes (as a datatype) isn't an issue, and the error states that it's a dimension issue but all of the independent components of the new time vec of (1xN) dimensions, which shouldn't have any issue being concantenated.
Any help in rectifying this is greatly appreciated... working remotely in this internship is killing me!
Accepted Answer
More Answers (1)
Antonio Ciociola
on 8 Aug 2020
The error that you get it's caused by the horzcat function.
Your datetime vector it's a row vector, therefore you have to use vertcat.
t2 = [t2(1:i-1); temp(k); t2(i:end)];
Categories
Find more on Dates and Time 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!