Buffer around a value in a matrix.

3 vues (au cours des 30 derniers jours)
SChow
SChow le 8 Déc 2022
Réponse apportée : Jonas le 8 Déc 2022
Hi I am struggling with building a buffer of values around a specific value in a matrix. For example I have a matrix
A=[ 0 0 0 0 1 0 0 1
1 0 0 0 0 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 0
1 0 0 0 1 1 0 0]
The intended matrix with a square buffer filled by 2 around all cells which are 1 (cells which are adjascent and diagonal). Thanks
B=[ 2 2 0 2 1 2 2 1
1 2 0 2 2 1 1 1
2 2 0 2 1 1 1 1
2 2 0 2 1 1 1 2
1 2 0 2 1 1 2 2]
  2 commentaires
Jonas
Jonas le 8 Déc 2022
i guess it should be
B=[ 2 2 0 2 1 2 2 1
1 2 0 2 2 1 1 1
2 2 0 2 1 1 1 1
2 2 0 2 1 1 1 2
1 2 0 2 1 1 2 2]
?
SChow
SChow le 8 Déc 2022
Modifié(e) : SChow le 8 Déc 2022
Yes, exactly. Sorry for the glitch there.
Corrected now

Connectez-vous pour commenter.

Réponse acceptée

Jonas
Jonas le 8 Déc 2022
A=[ 0 0 0 0 1 0 0 1
1 0 0 0 0 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 0
1 0 0 0 1 1 0 0];
B=imfilter(A,ones(3))
B = 5×8
1 1 0 1 2 3 4 3 1 1 0 2 4 6 7 5 1 1 0 2 5 8 8 5 1 1 0 3 6 8 6 3 1 1 0 2 4 5 3 1
B(B>0)=2
B = 5×8
2 2 0 2 2 2 2 2 2 2 0 2 2 2 2 2 2 2 0 2 2 2 2 2 2 2 0 2 2 2 2 2 2 2 0 2 2 2 2 2
B(A==1)=1
B = 5×8
2 2 0 2 1 2 2 1 1 2 0 2 2 1 1 1 2 2 0 2 1 1 1 1 2 2 0 2 1 1 1 2 1 2 0 2 1 1 2 2
isequal(B,[ 2 2 0 2 1 2 2 1
1 2 0 2 2 1 1 1
2 2 0 2 1 1 1 1
2 2 0 2 1 1 1 2
1 2 0 2 1 1 2 2])
ans = logical
1

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by