Replace and add element under specific condition

1 次查看(过去 30 天)
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]

回答(1 个)

Image Analyst
Image Analyst 2016-4-3
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

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by