フィルターのクリア

find element in cell

2 ビュー (過去 30 日間)
skysky2000
skysky2000 2017 年 7 月 12 日
コメント済み: dbmn 2017 年 7 月 13 日
Dear all, I've problem with the cell,
a= {[67 8 33] [1 2 3 5] [2 88 5] [3 66 7 90] [3] [66 78 12 1 44 6 77 3]},
how to find each cell have number 3. answer expect it is:
b= {[1 2 3 5] [3 66 7 90] [3] [66 78 12 1 44 6 77 3]}
thanks all

回答 (2 件)

dbmn
dbmn 2017 年 7 月 12 日
An alternative to KSSVs solution would be this one liner
b = a(cellfun(@(x) sum(x==3)>0, a));
  3 件のコメント
Jan
Jan 2017 年 7 月 12 日
The result of sum(x==3)>0 is unclear, when the cell element is the empty matrix. What about:
b = a(cellfun(@(x) any(x==3), a));
dbmn
dbmn 2017 年 7 月 13 日
Nice, using "any" there is a really neat idea.

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


KSSV
KSSV 2017 年 7 月 12 日
a= {[67 8 33] [1 2 3 5] [2 88 5] [3 66 7 90] [3] [66 78 12 1 44 6 77 3]} ;
b= {[1 2 3 5] [3 66 7 90] [3] [66 78 12 1 44 6 77 3]} ;
iwant = cell([],1) ;
count = 0 ;
for i = 1:length(a)
if any(a{i}==3)
count = count+1 ;
iwant{count} = a{i} ;
end
end
iwant

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by