Fill in Surrouding data

24 views (last 30 days)
Pete sherer
Pete sherer on 1 Nov 2024 at 15:45
Commented: Pete sherer on 1 Nov 2024 at 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

Accepted Answer

Dave B
Dave B on 1 Nov 2024 at 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>

More Answers (0)

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!