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

6 views (last 30 days)
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)

Answers (1)

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

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!