Groupcount function sort data in alphabetical order
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Gianluca Regina
el 8 de Jun. de 2022
Comentada: Jan
el 9 de Jun. de 2022
Dear MatLab users,
I have a cell array (attached) containing the IDs of my events. I used the groupcount function to see how many instances and how many events I had, and everything seemed to work fine. However, I just found out that the resulting variable containing the events sorted the data in alphabetical order. Thus, the counting also refers to the modified order, which is something I do not want. I managed to find the instances manually, but I also need the list in the original order. Is there a way to do using the groupcount function?
[ N , EVENTS ] = groupcounts(event_id); % EVENTS are in alphabetical order
0 comentarios
Respuesta aceptada
Jan
el 8 de Jun. de 2022
Editada: Jan
el 8 de Jun. de 2022
% groupcounts sorts the input:
C = {'C', 'C', 'C', 'A', 'A', 'E', 'E', 'E', 'E', 'B'}.';
[N, EVENTS] = groupcounts(C)
% Let N and EVENTS have the same order as in C:
[~, iC] = unique(C); % [EDITED, bug fixed]
[~, q] = sort(iC);
[sN, sEVENTS] = groupcounts(C);
N = sN(q)
EVENTS = sEVENTS(q)
If the equal keys are guaranteed to be neighboring:
% Call this instead of GROUPCOUNTS to keep the order
function [n, b] = RunLength_CStr(x)
x = x(:);
nx = numel(x);
d = [true; ~strcmp(x(1:nx-1), x(2:nx))]; % TRUE if values change
b = x(d); % Elements without repetitions
n = diff(find([d.', true])); % Number of repetitions
end
4 comentarios
Jan
el 9 de Jun. de 2022
There is no need for apologies. You are the only person who needs this function and I spend my time voluntarily to find solutions. Questions for clarifications are a standard part of solving problems.
I'm happy, if it is working now :-)
Más respuestas (0)
Ver también
Categorías
Más información sobre Time Series Events en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!