Check if a value is within one of the cells in a cell array

61 ビュー (過去 30 日間)
Adel Hafri
Adel Hafri 2022 年 3 月 9 日
For example, i have this cell array y and i want to see if 6 is included in index 1 of any of the cell arrays , how can i do that ?
y={[1,2],[2,3.51],[2,5],[2,6]};

回答 (1 件)

Jan
Jan 2022 年 3 月 9 日
編集済み: Jan 2022 年 3 月 9 日
Working with cells makes it harder. If you only want to know, if any 6 is contained:
y = {[1,2],[2,3.51],[2,5],[2,6]};
T = any(cat(2, y{:}) == 6)
T = logical
1
T = any(cat(2, y{:}) == 7)
T = logical
0
If all cell elements are [1 x 2] vectors, you could determine the cell index also:
X = cat(1, y{:});
[r,c] = find(X == 6)
r = 4
c = 2
If the cell contains arrays with not matching dimensions, a loop is an easy solution:
T = false;
for k = 1:numel(y)
if any(y{k}(:) == 6)
T = true;
break; % Leave loop at first match
end
end
  1 件のコメント
Sergio Rojas Blanco
Sergio Rojas Blanco 2022 年 8 月 14 日
I don't know if this worked to Adel but it did for me. Thank you Jan

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by