Finding the number of distinct letters in a string

15 visualizzazioni (ultimi 30 giorni)
Andrew il 19 Nov 2011
Commentato: lalonded il 14 Ott 2015
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 Commenti
Walter Roberson
Walter Roberson il 9 Set 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

Accedi per commentare.

Risposte (2)

Naz il 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):
for n=1:length(string)
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 Commenti
Andrew il 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

Accedi per commentare.

Image Analyst
Image Analyst il 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.
% 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));
% Display letters then character counts (all 1's if default input was used)
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 Commento
lalonded il 14 Ott 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!

Accedi per commentare.


Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by