Finding the number of distinct letters in a string

12 次查看(过去 30 天)
I'm trying to write a function that computes the number of occurrences of each letter in the string, and also the number of occurrences of adjacent pairs of characters in the string. I'm pretty stuck. Say if my input was a string, 'calculatorcalculator'...I'm trying to have my output show up something like
Number of c = 2
Number of a = 4
Number of l = 4
...
Number of ca = 2
Number of al = 2
...
and so on
I was trying to get something going with ...
function myprob(input)
number_of_c= length(input(ismember(input,'c')>0))
But I couldn't figure out how to assign anything to "not a variable", Should I use a cell array? I am just looking for some guidance here. I realize I may have stated my problem clearly but thanks for any help you can give
  3 个评论
Lalit Talele
Lalit Talele 2015-9-9
编辑:Walter Roberson 2015-9-9
I am trying to plot a histogram of a bit array, Lets say x = (1:1:1000) and i want to divide it into different parts such as 1-20, 21-40,.....,981-1000.
X = (1:1:1000)';
Edges = (1:20:1000)';
Count = histc(X,Edges);
hist(X,Edges)
This is the way i had proceed to count the bit error rate in different parts such as for example i had 10 bit error rate from 1-20 and so on.
But i am not able to find the solution.
Walter Roberson
Walter Roberson 2015-9-9
Lalit, you should have created a new Question for this.
In the case where the length of the array is evenly dividable by 20, then:
counts = sum( reshape(YourBitArray, 20, []) );
bar(counts); %draw the histogram

请先登录,再进行评论。

回答(2 个)

Naz
Naz 2011-11-20
For single letter detection I would create an array of 26 members. Now, assuming you have non-capital letters in the string you can just read each letter, convert it to number subtract 64 and count it as in histogram. For example, in the forloop if you get a letter 'a', that is 65 and you subtract 64, you get 1 (since 'a' is the first letter in the alphabet):
histogram=zeros(1,26);
for n=1:length(string)
currentLetter=string(n);
histogram(currentLetter-64)=histogram(currentLetter-64)+1;
end
Obviously, the code will not run as is, but I hope the idea is clear. So, as a result your array from 1to26 will contain number of occurrence for each letter from a to z respectively.
  3 个评论
Image Analyst
Image Analyst 2011-11-20
How about
for k = 1:26
fprintf('The count for letter %c is %d',...
k+64, histogram(k));
end
Note: you'll need to make this more robust if you want to handle both upper and lower case as well as other symbols, but this gives you the idea.
Andrew
Andrew 2011-11-20
I haven't tried this out yet. but thank you for the help. It's helping me think about how to approach more situations like this

请先登录,再进行评论。


Image Analyst
Image Analyst 2011-11-20
Try this simple demo:
% Ask user for their character string.
prompt = {'Enter the character string:'};
dlg_title = 'Enter letters'; % Needs to be short to be completely seen
num_lines = 1; % Lines per edit field.
defaultResponses = {'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^'};
response = inputdlg(prompt, dlg_title, num_lines, defaultResponses);
response = cell2mat(response)
if isempty(response)
% Bail out if they click Cancel.
return;
end
% Get list of unique characters entered.
allChars = unique(response)
% Go through the list of unique characters
% getting the count for each one.
for c = 1 : length(allChars)
countForThisChar = sum(response == allChars(c));
histogram(c) = countForThisChar;
fprintf('Counted %d occurrences of character %s\n', ...
countForThisChar, allChars(c));
end
% Display letters then character counts (all 1's if default input was used)
allChars
histogram
Note: It can handle any characters the user can input. The histogram bins for elements may not be at the same element number for subsequent runs but could be trivially adapted if you need that. This gives you just the count for what's there. If you always need "a" at bin 65 and "A" at bin 97 (their ascii values) then you'll need to do that simple adaptation.
  1 个评论
lalonded
lalonded 2015-10-14
^^
This should be the ACCEPTED ANSWER. Image Analyst's code is simple and counts the number of occurrences of ANY characters and displays them. Well done!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by