Borrar filtros
Borrar filtros

Adding continuously to get a sum of how many words there are

2 visualizaciones (últimos 30 días)
John Lutz
John Lutz el 29 de Sept. de 2017
Comentada: Steven Lord el 29 de Sept. de 2017
function getWords
str = char('jeff john 2td');
n = size(str,1);
for t=0
for i = 1:n
if str(i,1) == isletter(str(i,1))
t+1;
end
if str(i,1) == isnumeric(str(i,1))
disp('For', str(i,1), 'it isnt a word')
end
end
end
disp(t);
end
I want to continuously add +1 to my t value of originally 0 when I get another word into my string, but as of right now it keeps displaying "t" as 0 for some reason. Trying to get the function to be capable of displaying how many words there are, but if the one of the words starts with a number the function WILL NOT count it as a word. For example: if the string is 'my 2sons had lunch' it would display it as the t variable having 3 words.
Thank you for your assistance.
  1 comentario
Steven Lord
Steven Lord el 29 de Sept. de 2017
How many words would you say this sentence contains?
Jean-Luc counted to 3 then asked "Q, what are you doing here?"
Do you consider Jean-Luc to be zero, one, or two words? Does the hyphen disqualify it from being a word, split those two collections of characters into separate words, or join the two collections of characters together into one word?
Do you consider 3 to be a word?
Do you consider "Q, to be a word? In this context Q is the name of the character to whom Jean-Luc asked the question.

Iniciar sesión para comentar.

Respuesta aceptada

OCDER
OCDER el 29 de Sept. de 2017
Editada: OCDER el 29 de Sept. de 2017
NEW ANSWER: treats a word as one that starts with a letter
function wordcount = getWords
str = 'jeff john 2lion lion2 this2that dog.';
strcell = regexpi(str, '\w*', 'match');
%Defining word as one that starts with a letter (this2that would still be a word)
wordloc = cellfun(@(x) ~isempty(regexpi(x(1), '[a-z]')), strcell);
wordcount = sum(wordloc); %number of words
nonwordcount = sum(~wordloc); %number of non-words
%Displaying words / nonwords
fprintf('Is a word: %s\n', strcell{wordloc})
fprintf('Not a word: %s\n', strcell{~wordloc})
Outputs:
Is a word: jeff
Is a word: john
Is a word: lion2
Is a word: this2that
Is a word: dog
Not a word: 2lion
OLD ANSWER: One way to do this without loops:
function getWords
str = 'jeff john 2td tomorrow 3e4 a23 dog.';
strcell = regexp(str, '\s+', 'split');
wordloc = cellfun(@(x) isempty(regexp(x, '[^a-zA-Z\.]', 'once')), strcell); %gets location of string with only letter a-zA-Z
wordcount = sum(wordloc); %number of words, 4
nonwordcount = sum(~wordloc); %number of non-words
  5 comentarios
OCDER
OCDER el 29 de Sept. de 2017
I've updated the answer above that should answer this question. Hope this helps!
Cedric
Cedric el 29 de Sept. de 2017
Editada: Cedric el 29 de Sept. de 2017
As you are using regular expressions, note that the following would work too (here applied to your test str):
>> numel( regexp( str, '(^|\s)[a-zA-Z]' ))
ans =
5

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 29 de Sept. de 2017
t+1;
retrieves the value of t, adds 1 to the value, then throws away the value because the phrase ends in a semi-colon. You are not changing t. Perhaps you are thinking of C's t++ or t++1 which are not valid MATLAB syntax.
t = t + 1;

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by