Matching string from multiple arrays
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have three structures with time stamps (char arrays, eg '2016-11-02 10:00:00') and corresponding datapoints of significant wave heights (double), and these are of different length for the three structures so I cannot just concatenate them.
Basically I want to make a new array with only the datapoint values where the correcpodning time stamp exists in all three of my structures' time stamp field .
I've used ismember and matrix multiplication for just two of the structures (see below), but I realise that I still have values in the array I'm checking against that doesn't exist in the thing I'm checking, so I still have uneven lengths of the arrays. I have 163 triplets of stuctures to check, hence the 'i' to be able to run through all of them eventually.
Drifter_okera5 = ismember(cellstr(Drifters(i).ObsTimestamp), cellstr(era5(i).timestamp)); % check if what is in drifters are in era5
DrifterHsera5 = Drifter_okera5.*Drifters(i).Hs'; % Take the resulting logical matrix and multiply it with the drifter HS
DrifterHsok = DrifterHs(DrifterHs~= 0); %remove all the values that are now zeros, these do not have a matching era5 hs
Any tips are most welcome!
3 个评论
采纳的回答
Adam Danz
2020-9-8
编辑:Adam Danz
2020-9-10
Here's a demo that identies exact datetime matches. It assumes you've already converted the timestamps to datetime values instead of char using t = datetime(DateStrings,'InputFormat',infmt).
% Create demo data - all column vectors
A.timestamp = datetime(2020,1,1)+days(1:10)';
A.waveheight = rand(10,1);
B.timestamp = datetime(2020,1,1)+days(1:15)';
B.waveheight = rand(15,1);
C.timestamp = datetime(2020,1,1)+days(1:5)';
C.waveheight = rand(5,1);
% Find which datetime values *exactly* match in all 3 structures.
% dtInAll is a vector of datetime values that exist in all 3 strcutures.
dtInAll = intersect(intersect(A.timestamp,B.timestamp),C.timestamp);
Now you can get the indices of datetime matches for each structure and extract the waveheight values.
% waves is a nx3 matrix of waveheights where columns are defined by
% [A,B,C] structures and rows defined by deInAll.
waves = [A.waveheight(ismember(A.timestamp, dtInAll)), ...
B.waveheight(ismember(B.timestamp, dtInAll)), ...
C.waveheight(ismember(C.timestamp, dtInAll))];
Summarize the resuls in a table (or timetable)
T = table();
T.TimeStamp = dtInAll
T = [T, array2table(waves,'VariableNames',{'A','B','C'})];
% 5×4 table
% TimeStamp A B C
% ___________ _______ ________ _______
% 02-Jan-2020 0.94479 0.40391 0.64775
% 03-Jan-2020 0.49086 0.096455 0.45092
% 04-Jan-2020 0.48925 0.13197 0.54701
% 05-Jan-2020 0.33772 0.94205 0.29632
% 06-Jan-2020 0.90005 0.95613 0.74469
更多回答(1 个)
Steven Lord
2020-9-17
If you have your data stored in timetable arrays, call synchronize with your three timetable arrays as input and specify the newTimeBasis input as 'intersection'.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!