Azzera filtri
Azzera filtri

How do I find all the words with a certain letter, when I have a cell array of strings?

6 visualizzazioni (ultimi 30 giorni)
How do I find all the words with a certain letter(d), when I have a cell array of strings?
For example I have a shopping list. And I want to turn this into 0's and 1's. 1's being where a word is that contains this letter(d)

Risposte (1)

Image Analyst
Image Analyst il 5 Nov 2015
Try this:
ca = {'How' 'do' 'I' 'find' 'all' 'the' 'words' 'with' 'a' 'certain' 'letter'}
containsLetter = false(1, length(ca));
for k = 1 : length(ca)
if ~isempty(strfind(ca{k}, 'd'))
containsLetter(k) = true;
end
end
% Print to command window the indexes that have it
logicalIndexes = containsLetter
numericalIndexes = find(containsLetter)
See in command window:
ca =
'How' 'do' 'I' 'find' 'all' 'the' 'words' 'with' 'a' 'certain' 'letter'
logicalIndexes =
0 1 0 1 0 0 1 0 0 0 0
numericalIndexes =
2 4 7

Categorie

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