Filter löschen
Filter löschen

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

6 Ansichten (letzte 30 Tage)
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)

Antworten (1)

Image Analyst
Image Analyst am 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

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by