search a particular number in a cell array and return the index of the cell array that is found
23 views (last 30 days)
Show older comments
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 Comments
singh
on 13 Apr 2015
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
on 13 Apr 2015
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);
Accepted Answer
Oleg Komarov
on 10 Jul 2011
% 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 Comments
More Answers (2)
Andrei Bobrov
on 10 Jul 2011
correct
out = find(~cellfun(@(x)isempty(strfind(x(:)',157536)),cc.PixelIdxList))
Image Analyst
on 11 Jul 2011
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 Comment
Palash Dhande
on 6 Feb 2020
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?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!