Issue with data imputation, (inserting date time value into an array of dates).

6 ビュー (過去 30 日間)
Cmullica
Cmullica 2020 年 8 月 8 日
回答済み: Antonio Ciociola 2020 年 8 月 8 日
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!

採用された回答

Cmullica
Cmullica 2020 年 8 月 8 日
Ugh.... figured it out.... Leaving up for full disclosure instead of deleting the post:
The fix is as follows:
%%
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
The issue is here:
t2 = [t2(1:i-1); temp(k); t2(i:end)];
in my OP, the code was written as:
t2 = [t2(1:i-1), temp(k), t2(i:end)];
where in that last line it was trying to created a (1xN) vector but each individual component is in the form (Nx1).... I initially thought it was something funky going on with the datetime datatype...
amateur mistake over here I guess. Took me an embarassing 1.5 hours to find that.

その他の回答 (1 件)

Antonio Ciociola
Antonio Ciociola 2020 年 8 月 8 日
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)];

カテゴリ

Help Center および File ExchangeTime Series Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by