removing elements of cell array

9 ビュー (過去 30 日間)
James Connor
James Connor 2015 年 11 月 9 日
編集済み: William Smith 2018 年 4 月 3 日
say I have a 1 column cell array
x={'aa';'aal';'aalii';'aam';'aani';'aardvark';'aardwolf';'aaron'}
and word=nanny
how would I remove all words from the cell array that are not equal length to the word. For example. length(word)=5
remove all words from x that are not length 5

回答 (2 件)

Stephen23
Stephen23 2015 年 11 月 9 日
>> x = {'aa';'aal';'aalii';'aam';'aani';'aardvark';'aardwolf';'aaron'};
>> word = 'nanny';
>> x(numel(word)~=cellfun(@numel,x)) = []
x =
'aalii'
'aaron'
Use your favorite internet search engine to locate the official MATLAB documentation for the functions that I used. MATLAB has really great documentation, and you will be doing yourself a great favor by learning to use it.

William Smith
William Smith 2018 年 4 月 3 日
編集済み: William Smith 2018 年 4 月 3 日
If it's just explicit items you want to remove, rather based on a function, you can simply use 'setdiff'.
e.g.
setdiff({'one', 'two', 'buckle', 'my', 'shoe'},{'boot', 'my', 'shoe'})
ans =
1×3 cell array
'buckle' 'one' 'two'

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by