I have 2 columns in a matrix. 1st column has the dates. 2nd column has the values. How do I grab all values from 2nd column associated with its date from the 1st column??
3 次查看(过去 30 天)
显示 更早的评论
I have a much larger data set, but this is just a smaller sample size example of what I would like to do.
I want to grab all values from 4/1/18: 6
I want to grab all values from 4/2/18: 22, 4
I want to grab all values from 4/2/18: 2, 12, 8
Matrix Example:
Column 1 Column 2
4/1/18 6
4/2/18 22
4/2/18 4
4/3/18 2
4/3/18 12
4/3/18 8
0 个评论
回答(3 个)
Dyuman Joshi
2022-4-16
%example, not sure which date format and data type you are using
y={datetime('2018-01-04') 6;datetime('2018-02-04') 22;datetime('2018-02-04') 4;datetime('2018-03-04') 2;datetime('2018-03-04') 12;datetime('2018-03-04') 8}
z=unique(cellfun(@(x) x, y(:,1)))
for i=1:numel(z)
y(find(cellfun(@(x) x, y(:,1))==z(i)),2)'
%you can use curly parenthesis as well
end
0 个评论
Scott MacKenzie
2022-4-16
Something like this will work:
% test data
d = { '4/1/18', 6;
'4/2/18', 22;
'4/2/18', 4;
'4/3/18', 2;
'4/3/18', 12;
'4/3/18', 8 };
dt = datetime(d(:,1), 'InputFormat','MM/dd/yy');
value = cell2mat(d(:,2));
query1 = dt == datetime('4/1/18', 'InputFormat','MM/dd/yy');
v1 = value(query1)
query2 = dt == datetime('4/2/18', 'InputFormat','MM/dd/yy');
v2 = value(query2)
query3 = dt == datetime('4/3/18', 'InputFormat','MM/dd/yy');
v3 = value(query3)
0 个评论
Campion Loong
2022-5-18
% Using your small example
tt = timetable(datetime(2018,4,[1;2;2;3;3;3]), [6;22;4;2;12;8])
% Simply subscript to the data at the timestamp you are looking for,
% e.g. 4/2/2018
tt(datetime(2018,4,2),:)
% Between 4/2/2018 and 4/3/2018 (closed interval)
tt(timerange(datetime(2018,4,2), datetime(2018,4,3), 'closed'), :)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!