필터 지우기
필터 지우기

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

조회 수: 8 (최근 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
charlie
charlie 2011년 7월 10일
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;

댓글을 달려면 로그인하십시오.

추가 답변 (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