How to identify area of a matrix with same values

3 ビュー (過去 30 日間)
Mollymomo
Mollymomo 2018 年 10 月 19 日
回答済み: YT 2018 年 10 月 19 日
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.

回答 (1 件)

YT
YT 2018 年 10 月 19 日
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)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by