# How can I make diamond shape with a matrix?

25 views (last 30 days)
Coeing on 25 Nov 2019
Answered: Stephen Cobeldick on 26 Nov 2019
I want to make diamond image with a matrix
ex. background image is zeros(1000,1000)
from center, make this matrix [1 2 ; 3 4] like a diamond..
.
.
.
0 0 1 2 0 0...
0 0 3 4 0 0
1 2 1 2 1 2
3 4 3 4 3 4
0 0 1 2 0 0
0 0 3 4 0 0 ...
.
.
.
plz help me.. thanks!!

Andrei Bobrov on 25 Nov 2019
Edited: Andrei Bobrov on 26 Nov 2019
a = strel('diamond',250);
out = kron(a.Neighborhood,[1 2 ; 3 4]);

#### 1 Comment

Andrei Bobrov on 26 Nov 2019
I'm fixed typo.

Stephen Cobeldick on 26 Nov 2019
Without the image toolbox:
>> N = 5;
>> V = round((N:-1:1)/(N+1));
>> M = toeplitz(V);
>> M = M & rot90(M)
M =
0 0 1 0 0
0 1 1 1 0
1 1 1 1 1
0 1 1 1 0
0 0 1 0 0
>> kron(M,[1,2;3,4])
ans =
0 0 0 0 1 2 0 0 0 0
0 0 0 0 3 4 0 0 0 0
0 0 1 2 1 2 1 2 0 0
0 0 3 4 3 4 3 4 0 0
1 2 1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4 3 4
0 0 1 2 1 2 1 2 0 0
0 0 3 4 3 4 3 4 0 0
0 0 0 0 1 2 0 0 0 0
0 0 0 0 3 4 0 0 0 0

Akira Agata on 26 Nov 2019
N = 5; % <- Should be odd number
A = repmat([1 2;3 4],N);
se = strel('diamond',floor(N/2));
idx = imresize(se.Neighborhood,2);
A(~idx) = 0;
>> A
A =
0 0 0 0 1 2 0 0 0 0
0 0 0 0 3 4 0 0 0 0
0 0 1 2 1 2 1 2 0 0
0 0 3 4 3 4 3 4 0 0
1 2 1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4 3 4
0 0 1 2 1 2 1 2 0 0
0 0 3 4 3 4 3 4 0 0
0 0 0 0 1 2 0 0 0 0
0 0 0 0 3 4 0 0 0 0