How to find the position of a number in an cell-array?

I have a cell array P= {[2,5], [3], [], [1,14], [], [9]}. I want to find out the number of cell where value 14 is located. Like, here 14 is located inside the cell number 4.
How can I do that?

2 comentarios

how do i create my own question ?
Hi yaish, you need to go 'ask' in the top.

Iniciar sesión para comentar.

 Respuesta aceptada

KSSV
KSSV el 26 de Feb. de 2021
Editada: KSSV el 26 de Feb. de 2021
P= {[2,5], [3], [], [1,14], [], [9]} ;
N = length(P) ;
val = 14 ;
for i = 1:N
idx = ismember(14,P{i}) ;
if any(idx)
break
end
end
i
P{i}

5 comentarios

Nazmun Nahar Khan
Nazmun Nahar Khan el 26 de Feb. de 2021
Editada: Nazmun Nahar Khan el 26 de Feb. de 2021
its not working. I got P{i}= 1 14. But it should be cell number 4
KSSV
KSSV el 26 de Feb. de 2021
i is the cell number friend. P{i} gives you the values in the cell.
Thank you. I got it.
I also find a another way, just sharing here.
out = cellfun(@(x) find(x == 14),P,'un',0);
idx = cellfun('isempty',out);
i=find(~idx)
KSSV
KSSV el 26 de Feb. de 2021
Editada: KSSV el 26 de Feb. de 2021
cellfun uses loop behind. find will be slow.
Thanks

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 26 de Feb. de 2021

Comentada:

el 26 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by