Character Frequency of String
31 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I'm trying to figure out how to get groups and group counts for a string.
For example, if you input
string = "SunnyDay"
the output would be:
S: 1
U: 1
N: 2
Y: 2
D: 1
A: 1
3 comentarios
Paul
el 17 de Sept. de 2022
I was quite curious as to how this Question popped up today as if it had been asked on 5 Nov 2021 with no activity since then. Any idea how that happened?
Respuestas (2)
Paul
el 17 de Sept. de 2022
Using the new dictionary in 2022b and the on-point example from this blog post
str = "SunnyDay";
chr = char(str);
d = dictionary(string.empty,double.empty);
for c = chr
if isKey(d,c) % If this char exists in the dictionary
d(c) = d(c) +1; % Increment the value associated with that char by 1.
else
d(c) = 1; % Initialise a new char in the dictionary with the value set to 1.
end
end
d
I tried making the dictionary key a char, but that resulted in an error.
0 comentarios
Sayan
el 25 de Nov. de 2022
str = "SunnyDay";
chr = char(str);
d = dictionary(string.empty,double.empty);
for c = chr
if isKey(d,c) % If this char exists in the dictionary
d(c) = d(c) +1; % Increment the value associated with that char by 1.
else
d(c) = 1; % Initialise a new char in the dictionary with the value set to 1.
end
end
0 comentarios
Ver también
Categorías
Más información sobre Logical 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!