How to find index of a value in cell array

32 Ansichten (letzte 30 Tage)
Vishal Sharma
Vishal Sharma am 25 Jan. 2017
Kommentiert: Walter Roberson am 8 Nov. 2024
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

Akzeptierte Antwort

the cyclist
the cyclist am 25 Jan. 2017
Bearbeitet: the cyclist am 25 Jan. 2017
isSix = cellfun(@(x)isequal(x,6),A);
[row,col] = find(isSix);
  4 Kommentare
Kristoffer Walker
Kristoffer Walker am 13 Dez. 2019
Not at all intuitive, but it works great, even for logicals.
JianLin Sun
JianLin Sun am 8 Nov. 2024
It's useful,thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 25 Jan. 2017
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 Kommentar
Walter Roberson
Walter Roberson am 8 Nov. 2024
Looking again several years later, it looks like the code should use
[maxrow, maxcol] = find( cell2mat(B) == 6);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by