Find in a cell array?
1,219 次查看(过去 30 天)
显示 更早的评论
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 个评论
Elias Berra
2015-11-17
X = my_array_data [row,col] = find(X==21) %In this example, it retrieves the cell location which contains the value 21.
Marwan Malaeb
2022-5-20
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.
采纳的回答
Jan
2013-8-7
编辑:Jan
2013-8-7
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 个评论
Jan
2013-8-8
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
2017-8-16
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!
更多回答(2 个)
Bill Tubbs
2022-2-15
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)
Caroline
2013-8-7
编辑:Azzi Abdelmalek
2013-8-7
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 个评论
Filza Ashraf
2014-5-22
how can i find a pixel intensity if cell contains an image or image is stored in cell???
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!