How to count certain characters in a word?

6 次查看(过去 30 天)
I need to write a program, using loops and functions, that would count numbers of certain letters in a word.
For example if i input a word:'matlabprogram', and i want to count number of a,b,c,d letters in this word the program would say: a=3,b=3,c=0,d=0
Can you please help me?
P.S. sorry for my bad english

采纳的回答

Andrei Bobrov
Andrei Bobrov 2013-4-2
a = ('a':'d')'
b = 'matlabprogram'
out1 = [cellstr(a), num2cell(histc(b,a)')]

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-4-2
word='matlabprogram'
Lettre='abcd'
for k=1:numel(Lettre)
m=Lettre(k);
assignin('base',m,sum(ismember(word,m)))
end
  1 个评论
Jan
Jan 2013-4-2
Although the OP asked for the names "a", "b", ... this is not a smart idea and I'd definitely avoid suggesting assignin.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by