How can I threshold a matrix and get the coordinates of those values?

9 views (last 30 days)
Native on 10 Dec 2018
Edited: Guillaume on 10 Dec 2018
I have a matrix, X with dimensions 132X132, with values ranging from -0.1 to 0.4. I'd like to threshold it to values above 0.2, and most importantly, obtain the coordinates to which those values belonged to in the previous matrix.
Thank you.

MUHAMMED IRFAN on 10 Dec 2018
Edited: MUHAMMED IRFAN on 10 Dec 2018
ind = find(X>.2);
This gives the index of all the points in X greater than 0.2 using linear indexing.
To convert the single indices to subscripts,
[I,J] = ind2sub([132,132],ind);
This gives x and y coordinates of all points with values greater than 0.2
To get the values of all those points,Go
myPoints = X(find(X>0.2));
Stephen23 on 10 Dec 2018
Note that indexing with one index is called linear indexing in the MATLAB documentation:
Guillaume on 10 Dec 2018
Edited: Guillaume on 10 Dec 2018
If you want 2D coordinates out of find just ask for them rather than getting linear indices and converting them into 2d indices:
[row, column] = find(X > 0.2); %no need for ind = find(X>0.2) followed by [row, column] = ind2sub(...)
Similarly, find can also give you the values. It's the third output:
[row, column, value] = find(X > 0.2); %much faster than X(find(X>0.2))
Also, note that as a filter, find is never needed. You can use the logical array input directly as a filter, so:
X(find(X > 0.2))
is a waste of time,
X(X > 0.2)
is faster and simpler.
Finally, it's never a good idea to hardcode the size of the matrices. If you had to use ind2sub,
[row, column] = ind2sub(size(X), ind); %instead of ind2sub([132 132], ind)
would be a lot safer.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!