Find a letter position within a word.

2 vues (au cours des 30 derniers jours)
JEE
JEE le 18 Avr 2018
Commenté : Walter Roberson le 20 Avr 2018
I need to right a function that will take in a word and a letter. Then return a list of all the positions in word where the letter exists. I need to figure out how to do this without using any built in functions. So far I have :
function result = find_letter_positions(word,letter)
indexes = [];
for i = 1:length(word)
if word(i) = = letter
after this I am unsure of where to go
  3 commentaires
JEE
JEE le 18 Avr 2018
Correct
Walter Roberson
Walter Roberson le 20 Avr 2018
Please do not close Questions that have an Answer

Connectez-vous pour commenter.

Réponse acceptée

Birdman
Birdman le 18 Avr 2018

This might help you:

function indexes = find_letter_positions(word,letter)
indexes = zeros(1,numel(word));
for i = 1:numel(word)
    if word(i)==letter
        indexes(i)=i;
    end
end
indexes=indexes(indexes~=0);
end
  1 commentaire
Stephen23
Stephen23 le 18 Avr 2018
Note that zeros, numel, and indexing (using subsref) are all inbuilt functions.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by