フィルターのクリア

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;
Oleg Komarov
Oleg Komarov 2011 年 7 月 10 日
Changed it to:
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