How to find index of a value in cell array

30 ビュー (過去 30 日間)
Vishal Sharma
Vishal Sharma 2017 年 1 月 25 日
コメント済み: Walter Roberson 2024 年 11 月 8 日
I have this Cell Array ‘A’ of size 3 by 7
A = { 3 4 [] [] [] [] []
2 6 -2 2 -2.1 2 2
-5 -5 25 1 [] [] []}
I want to find index of ‘6’ element in 2nd row and 2nd column The answer shall be row = 2 and column = 2

採用された回答

the cyclist
the cyclist 2017 年 1 月 25 日
編集済み: the cyclist 2017 年 1 月 25 日
isSix = cellfun(@(x)isequal(x,6),A);
[row,col] = find(isSix);
  4 件のコメント
Kristoffer Walker
Kristoffer Walker 2019 年 12 月 13 日
Not at all intuitive, but it works great, even for logicals.
JianLin Sun
JianLin Sun 2024 年 11 月 8 日
It's useful,thanks.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 1 月 25 日
Re-using the framework of my answer to your earlier question:
B = A;
B(cellfun(@isempty, B)) = {NaN};
[maxrow, maxcol] = find( reshape(cell2mat(B), [], 1) == 6);
  1 件のコメント
Walter Roberson
Walter Roberson 2024 年 11 月 8 日
Looking again several years later, it looks like the code should use
[maxrow, maxcol] = find( cell2mat(B) == 6);

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

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by