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??
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (3)
Dyuman Joshi
el 16 de Abr. de 2022
%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 comentarios
Scott MacKenzie
el 16 de Abr. de 2022
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 comentarios
Campion Loong
el 18 de Mayo de 2022
% 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 comentarios
Ver también
Categorías
Más información sobre Calendar en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!