Fill in Surrouding data

24 次查看(过去 30 天)
Pete sherer
Pete sherer 2024-11-1,15:45
评论: Pete sherer 2024-11-1,17:19
Hi,
I have a matrix with bit mask [0, 1]. Is there are quick way to fill surrounding 1 data with value 1.
For example, with
ttmp =
[0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 1 0 1 0 0 0 0
];
I want to turn in into
ttmp2=
[0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 1 0 1 1 1 0 0
0 0 1 1 1 1 1 0 0 0]
Thanks much

采纳的回答

Dave B
Dave B 2024-11-1,15:55
You could use conv2 with little kernel and then turn "overlapping" 2's into ones:
ttmp = [0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 1 0 1 0 0 0 0]
ttmp = 7×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
tconv = conv2(ttmp,[0 1 0;1 1 1;0 1 0],'same')
tconv = 7×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 2 1 1 0 0 0 0 1 1 2 1 2 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ttmp2 = double(tconv>0)
ttmp2 = 7×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by