Groupcount function sort data in alphabetical order

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

 채택된 답변

Jan
Jan 2022년 6월 8일
편집: Jan 2022년 6월 8일
% groupcounts sorts the input:
C = {'C', 'C', 'C', 'A', 'A', 'E', 'E', 'E', 'E', 'B'}.';
[N, EVENTS] = groupcounts(C)
N = 4×1
2 1 3 4
EVENTS = 4×1 cell array
{'A'} {'B'} {'C'} {'E'}
% 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)
N = 4×1
3 2 4 1
EVENTS = sEVENTS(q)
EVENTS = 4×1 cell array
{'C'} {'A'} {'E'} {'B'}
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

Gianluca Regina
Gianluca Regina 2022년 6월 8일
편집: Gianluca Regina 2022년 6월 8일
I tried the code you sent me but it didn't work, also I'm not sure what that function is for. In my case, the variable is such that the order is like { 'C' , 'C' ,'C' , 'A' , 'A' , 'E' , 'E' ,'E' , 'E' , 'B' } (see attached variable). There are no repetitions of 'C' after the first three, and so on. My desidered output is:
EVENTS = { 'C' , 'A' , 'E' , 'B' }
N = [ 3 , 2 ,4 , 1 ]
I saw that the "unique" function has the "sorted" and "stable" options, can they help?
There is a problem with my suggestion for groupcounts. I'm fixing it.
[EDITED: Done, see my answer]
"it didn't work" - this is a weak description of the problem you have with the code. Please take the time to explain the probelm with details. It is easier to fix a bug than to guess, what the bug is.
"I'm not sure what that function is for" - The function does, what you are asking for.
C = { 'C' , 'C' ,'C' , 'A' , 'A' , 'E' , 'E' ,'E' , 'E' , 'B' };
[N, EVENTS] = RunLength_CStr(C)
N = 1×4
3 2 4 1
EVENTS = 4×1 cell array
{'C'} {'A'} {'E'} {'B'}
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
I apologize, as I was writing that it didn't work (the old code gave the same results of groupcounts, so still no alphabetical order) you edited the answer and added the function, which at first glance is not immediate to understand (at least for me). Nonetheless, your code works perfectly now, thank you very much!
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 :-)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2022년 6월 8일

댓글:

Jan
2022년 6월 9일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by