How can I threshold a matrix and get the coordinates of those values?
28 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
MUHAMMED IRFAN
2018-12-10
编辑:MUHAMMED IRFAN
2018-12-10
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));
2 个评论
Stephen23
2018-12-10
Note that indexing with one index is called linear indexing in the MATLAB documentation:
Guillaume
2018-12-10
编辑:Guillaume
2018-12-10
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!