Hello all,
Suppose a cell array 10x1 consisted of random numbers from 1 to 5. How can I find the locations for number 5?
All the best,
MhD

3 comentarios

Jan
Jan el 7 de Ag. de 2013
I've deleted the duplicate question.
Elias  Berra
Elias Berra el 17 de Nov. de 2015
X = my_array_data [row,col] = find(X==21) %In this example, it retrieves the cell location which contains the value 21.
Marwan Malaeb
Marwan Malaeb el 20 de Mayo de 2022
call this array for example X
type k=find(X==5)
it will return for you the number of the cell that has the value of 5.

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 7 de Ag. de 2013
Editada: Jan el 7 de Ag. de 2013

78 votos

C = {1,5,3,4,2,3,4,5,2,1};
index = find([C{:}] == 5);
Here [C{:}] is a faster inlined version of cell2mat.
Alternative:
index = cellfun(@(x) x==5, C, 'UniformOutput', 1);
Or the long and most likely faster form:
index = false(1, numel(C))
for k = 1:numel(C)
index(k) = (C{k} == 5);
end
[EDITED] If you are talking of a cell string, this is much faster:
D = {'1' '5' '3' '4' '2' '3' '4' '5' '2' '1'};
index = find(strcmp(D, '5'));

5 comentarios

M G
M G el 7 de Ag. de 2013
Hey Jan,
Nice help. I just do not understand the difference between following two: C=[1] [5] [3] [4] [2] [3] [4] [5] [2] [1] ... which is what you said in your example and:
D='1' '5' '3' '4' '2' '3' '4' '5' '2' '1'
which I have the problem with. Both of them are cell arrays.However, your suggested way unfortunately doesn't work with "D".
Any idea to help me understand is appreciated :)
All the best....
Jan
Jan el 7 de Ag. de 2013
Please do not write only "does not work", but explain what happens instead: Do you get an error message or does the result differ from your expectations?
It would be useful, if you post the real data directly to the question, because then the other users do not waste your and their time with not matching suggestions. Not that "D='1' '5'..." is not valid Matlab syntax, because the surrounding braces are essential.
While I expect my code to work with D fluently, when you search for the character '5' instead of the number 5. But Matlab offers a much faster solution then, see [EDITED]
M G
M G el 8 de Ag. de 2013
strcmp works! Thanks. Here is data:
https://dl.dropboxusercontent.com/u/19202474/eheader.zip
Here is how it worked based on what you suggested.
S = char(cellfun(@(x) num2str(x),eheader,'Un',0))
Index = [];
j=1;
for i=1:size(S,1)
if(strcmp(S(i,:),' -88 ')==1)
Index(j) = i;
j = j+1;
end
end
Jan
Jan el 8 de Ag. de 2013
This looks strange. When eheader is numerical, converting it elementwise inside a cellfun call to a string and afterwards by char to a char matrix is cruel. Then assuming a certain number of spaces around the value is fragile, because the width depends on the values. What about this (I cannot open the posted MAT file, better post code in the forum which creates the example data):
x = find([eheader{:}] == -88)
STRCMP works much faster with cell strings, so at least do not let CHAR() create a CHAR-matrix.
Kylie Hansen
Kylie Hansen el 16 de Ag. de 2017
Just a casual MATLAB coder dropping by this older thread on a hunt for answers. Your response for the cell string method worked easily for me. Thank you so much for including it!

Iniciar sesión para comentar.

Más respuestas (2)

Bill Tubbs
Bill Tubbs el 15 de Feb. de 2022

7 votos

Just in case someone comes here looking to do this with a cell array of chars as I was, it's quite easy this way:
my_cell_array = {'a', 'b', 'c'};
i = find(strcmp(my_cell_array, 'b'));
assert(i == 2)

1 comentario

hongyi xu
hongyi xu el 17 de Abr. de 2022
Genius! Your supplement exactly fits my question.

Iniciar sesión para comentar.

Caroline
Caroline el 7 de Ag. de 2013
Editada: Azzi Abdelmalek el 7 de Ag. de 2013

1 voto

cellarray_new = zeros; %initializing the array
ind = 1; %indices for new array
for j = 1:10
if (cellarray(j) == 5)
cellarray_new(ind) = j;
ind = ind + 1;
end
end
the array cellarray_new will contain all the indices of the original cell array that contain the number 5

3 comentarios

Azzi Abdelmalek
Azzi Abdelmalek el 7 de Ag. de 2013
Why initializing cellarray_new to 0?
Jan
Jan el 7 de Ag. de 2013
I assume that "cell array" implies, that the array is a cell.
Filza Ashraf
Filza Ashraf el 22 de Mayo de 2014
how can i find a pixel intensity if cell contains an image or image is stored in cell???

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

M G
el 7 de Ag. de 2013

Comentada:

el 20 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by