How to identify area of a matrix with same values
3 次查看(过去 30 天)
显示 更早的评论
I have a matrix of 10x10 filled with 0s and 1s. There are some areas of 0s enclosed by areas of 1s. I would like to be able to identify specific areas of 0s.
I am using ginput, so I would like to select a 0 from the matrix, and then I want Matlab to identify the area surrounding that specific 0. I believe this can be done with conv2, but I don't know how.
0 个评论
回答(1 个)
YT
2018-10-19
This question is similar to this one asked before ( https://mathworks.com/matlabcentral/answers/402080-how-to-find-the-neighbors-of-each-element-in-a-matrix#answer_321522)
So if I understand it corect you want something like this
%small 5x5 example matrix
A = [0 1 0 0 0;
1 1 0 0 0;
0 0 1 1 1;
0 0 1 0 1;
0 0 1 1 1];
%create matrix with zeros with size of A
M = zeros(size(A));
%use the coordinates from ginput; M(row,col)
M(1,1) = 1;
%convolution
C = conv2(M,[1,1,1;1,0,1;1,1,1],'same')>0;
%return indices
[row col] = find(C>0);
%return values (which in your case isnt really interesting cuz will only returns 1's)
A(C)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!