Replace and add element under specific condition

1 view (last 30 days)
if i have this a (n,m) tow matrix A and B for example
A = [ 1 0 1 0 1
0 1 0 1 1
1 1 1 0 0
1 0 0 1 1
1 1 0 0 1 ]
B = [ 1 0 1 0 1
0 0 0 0 1
1 0 0 0 0
1 0 0 0 1
1 1 0 1 1]
I need function to do this steps
w = B(1,:) % extract first row
for k:1:size(w)
if there is one in w(k) then go back to Matrix A(1,k)
chick if there is a one's beside it >> true
set in Matrix B(k) these one's
for example B(3,:) is [1 0 0 0 0] then go back to A(3,:) = [1 1 1 0 0]
then we check if there are one's neighbors to that one >> if yes
then put the same number of one's beside the first one and update B(3,:)
so B(3,:) will be [1 1 1 0 0]
  • Note : can't we say B(3,:) | A(3,:) because for example
the B(end,:) = [ 1
1
0
1
1 ]
then we chick A(2,end) [0 1 0 1 1] >> we see there is a just one beside it so the update on the second row in B will be like this [ 0 0 0 1 1]

Answers (1)

Image Analyst
Image Analyst on 3 Apr 2016
How about this, which uses morphological reconstruction:
marker = B;
mask = A;
C = imreconstruct(marker,mask, 4)
C =
1 0 1 0 1
0 1 0 1 1
1 1 1 0 0
1 0 0 1 1
1 1 0 0 1

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!