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

23 views (last 30 days)
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
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
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);

Sign in to comment.

Accepted Answer

Oleg Komarov
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
charlie
charlie on 10 Jul 2011
thank you for your help. I got the following error when trying out the code:
??? The right hand side of this assignment has too few
values to satisfy
the left hand side.
Error in ==> charlie at 54
out{idx} = 0;

Sign in to comment.

More Answers (2)

Andrei Bobrov
Andrei Bobrov on 10 Jul 2011
correct
out = find(~cellfun(@(x)isempty(strfind(x(:)',157536)),cc.PixelIdxList))
  1 Comment
charlie
charlie on 10 Jul 2011
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))
>>

Sign in to comment.


Image Analyst
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
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?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!