Find rows and columns in matrix that meet a condition, fast
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Matlab_user
am 10 Aug. 2016
Beantwortet: RMello
am 17 Apr. 2017
I have a large matrix S (could be as large as 1920x1080 pixels in an image) that contains labels assigned to patches of varying sizes within that image. It looks like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/155629/image.png)
As it can be seen, S is divided into blocks labelled with uint32 numbers. Then I need to find the rows and columns that are equal to say 4803. The standard Matlab approach would be:
[m, n] = find(S == 4803);
To which it should return:
m =
5
6
5
6
n =
3
3
4
4
This operation gets called thousands of times and costs me hours of calculation. I have also experimented with bsxfun and it is still very slow. Can anyone suggest a faster way to accomplish this?
1 Kommentar
Stephen23
am 10 Aug. 2016
Would it be possible to use logical indexing, or is it strictly required to get the row and column indices ?
Akzeptierte Antwort
Sean de Wolski
am 10 Aug. 2016
How about:
bwconncomp()
This returns a list of pixel indices, from which you can calculate row/column and size. You can also pass it into regionprops to do that for you. Alternatively, just call regionprops directly asking for 'Area' and 'PixelIdxList'.
Not sure about speed but it's something to consider/try.
4 Kommentare
Image Analyst
am 10 Aug. 2016
"this function only working with BW images" <== that is incorrect. In fact, labeled images are of class double, not logical.
Image Analyst
am 10 Aug. 2016
Tell us what the numbers represent - image values or labels (blob ID numbers). And give us context: what are you going to do once you have the knowledge of all locations where a certain value resides? There may be a better way. There are a lot of imaging functions and Sean and I know virtually all of them and one might be the perfect thing if we just knew what you ultimately wanted to do.
Weitere Antworten (1)
RMello
am 17 Apr. 2017
if you want to find a row in matrix A that all elements are 4803 and your rows have 1080 elements, you do:
find(sum(A')==1080*4083)
if you want to find a column you take the ' off.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!