search a particular number in a cell array and return the index of the cell array that is found

30 次查看(过去 30 天)
Hi guys, thanks for reading. i am new to M, and have a quick question.
I have a cell array, cc.PixelIdxList which is a 1x100 cell array, in each cell it contains a list of numbers. for example:
index 1 contains: 156534 157525 157526 157527 157528
index 2 contains: 157531 157532 157533 157534 157535 157536
I want to use a function to find a number and return the index of the cell in the cc.PixelIdxList.
use=find([cc.PixelIdxList{:}]==157536);
Could anyone give me a hand with this? many thanks.
  2 个评论
singh
singh 2015-4-13
my problem is same
if have array which only two columns
X Y
10.2 12.3
21.443 81.31
32.23 43.32so on
than how to get index value after find the data
suppose i have X value 21.443 and Y value 81.31
how to get the index value 2
Image Analyst
Image Analyst 2015-4-13
Try something like this
x = xy(:, 1); % Get x alone from the first column
% Find distance of all x from the value 21.443
distances = sqrt(abs(x-21.443));
% Find the index of the closest, the minimum distance
[minDistance, indexOfClosest] = min(distances);

请先登录,再进行评论。

采纳的回答

Oleg Komarov
Oleg Komarov 2011-7-10
% Index to the number within the cell
out = cellfun(@(x) find(x == 157536),cc.PixelIdxList,'un',0);
% Index to the cell
idx = cellfun('isempty',out);
find(~idx)
% Pad with zeros the empty cells in the first index
out(idx) = {0}
out = [out{:}];

更多回答(2 个)

Andrei Bobrov
Andrei Bobrov 2011-7-10
correct
out = find(~cellfun(@(x)isempty(strfind(x(:)',157536)),cc.PixelIdxList))
  1 个评论
charlie
charlie 2011-7-10
thanks for the answer, i got the following error, maybe it is because the content of a cell is not string but a uint16?
??? Error using ==> strfind
Input strings must have one row.
Error in ==> @(x)isempty(strfind(x,157536))
Error in ==> chao at 44
out =
find(~cellfun(@(x)isempty(strfind(x,157536)),cc.PixelIdxList))
>>

请先登录,再进行评论。


Image Analyst
Image Analyst 2011-7-11
Charlie, I think you're totally taking the wrong approach. PixelIdxList (returned by regionprops) is the linear index (look it up) of a pixel in an image. How would you possibly know to pick a value for it? For example, if PixelIdxList were 517, then that would refer to the element at (2, 2) of a 512x512 image. Let's say that blob #1 contained that pixel. So you're basically asking how you can stick in 517 (which you came up with who-knows-how) and get out 1. I don't think you'd ever do that. Explain what you really want to do. For example do you want to know which blob (region) is the one that contains a particular (x,y) coordinate? Do you want the x,y coordinates themselves instead of the linear indices? If so, ask for PixelList instead of PixelIdxList. I just think you're trying to do some complicated thing to get something that is achieved in a much simpler way, if only you knew more about how regionprops worked.
  1 个评论
Palash Dhande
Palash Dhande 2020-2-6
Hi Image Analyst,
something you mention in your comment is exactly what I'm trying to achieve.
From the output of bwconncomp, i would like to find the blob inside which a particular (predefined) pixel lies. This pixel is defined by a row and a column.
Any clues how to go about doing this?

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by