How do I use use logical relations on a character array?

Say like I have an array animals=[cat;dog;hamster;shark;duck;bat]. How do I get an array that only contains animals that is spelled with 4 or more letters?

 採用された回答

Stephen23
Stephen23 2017 年 2 月 15 日

0 投票

>> a = {'cat';'dog';'hamster';'shark';'duck';'bat'};
>> a(cellfun('length',a)>3)
ans =
'hamster'
'shark'
'duck'

3 件のコメント

Anonymous Matrix
Anonymous Matrix 2017 年 2 月 15 日
編集済み: Anonymous Matrix 2017 年 2 月 15 日
do i have to type out each one? because the example i gave is short and simple. but i actually have to work with a given array (that i can't manipulate) that has 54 total.
Stephen23
Stephen23 2017 年 2 月 15 日
編集済み: Stephen23 2017 年 2 月 15 日
@Anonymous Matrix: no you do not have to type out each name again. I used the variable name a as an example. Use whatever variable you have been given, e.g.:
name_of_your_variable(cellfun('length',name_of_your_variable)>3)
Anonymous Matrix
Anonymous Matrix 2017 年 2 月 15 日
thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by