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

Paul
Paul 2022년 9월 17일
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?
Stephen23
Stephen23 2022년 9월 17일
"Any idea how that happened?"
Because of Kuchi's comment.

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

답변 (2개)

Paul
Paul 2022년 9월 17일

1 개 추천

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
d =
dictionary (stringdouble) with 6 entries: "S" ⟼ 1 "u" ⟼ 1 "n" ⟼ 2 "y" ⟼ 2 "D" ⟼ 1 "a" ⟼ 1
I tried making the dictionary key a char, but that resulted in an error.
Sayan
Sayan 2022년 11월 25일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

질문:

2021년 11월 5일

답변:

2022년 11월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by