Replace and add element under specific condition

조회 수: 1 (최근 30일)
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 4월 3일
댓글: Firas Al-Kharabsheh 2016년 4월 4일
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
  댓글 수: 3
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 4월 4일
no Mr Walter the c(2,2) must be zero ,, because its not neighbor to the last one in this row ,, there is zero between them

댓글을 달려면 로그인하십시오.

카테고리

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