Finding the number of distinct letters in a string

21 views (last 30 days)
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 Comments
Lalit Talele
Lalit Talele on 9 Sep 2015
Edited: Walter Roberson on 9 Sep 2015
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 on 9 Sep 2015
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

Sign in to comment.

Answers (2)

Naz
Naz on 20 Nov 2011
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 Comments
Image Analyst
Image Analyst on 20 Nov 2011
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 on 20 Nov 2011
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

Sign in to comment.


Image Analyst
Image Analyst on 20 Nov 2011
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 Comment
lalonded
lalonded on 14 Oct 2015
^^
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!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!