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??
2 ビュー (過去 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!