How can I make diamond shape with a matrix?

19 views (last 30 days)
Coeing
Coeing on 25 Nov 2019
Answered: Stephen23 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!!

Answers (3)

Stephen23
Stephen23 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

Andrei Bobrov
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]);

Akira Agata
Akira Agata on 26 Nov 2019
How about the following?
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

Tags

Community Treasure Hunt

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

Start Hunting!