How do I change values in an array based upon its previous value?

2 views (last 30 days)
Hi all! I'm new to MATLAB.
So basically I have a 2D array and a spreading pattern/fractal that I wish to execute, I can make the pattern happen when turning zeros to ones but then it get's messy after that. the image is a basic rundown of what I want to happen for each iteration of the pattern.
Thank you, Michael.Pattern MATLAB.PNG
  1 Comment
Guillaume
Guillaume on 15 May 2019
Why isn't the pattern spread around the 1s in step 2? I.e. why isn't that last matrix:
0 0 1 0 0
0 1 2 1 0
1 2 3 2 1
0 1 2 1 0
0 0 1 0 0

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 15 May 2019
Edited: Guillaume on 15 May 2019
Assuming you've made a mistake in your second step (see comment), this is trivially achieved with imdilate (requires image processing toolbox
A = [0 0 0 0 0; 0 0 0 0 0;0 0 1 0 0; 0 0 0 0 0; 0 0 0 0 0]
nstep = 5;
for step = 1:nstep
A = imdilate(A > 0, [0 1 0; 1 1 1;0 1 0]) + A
end
  3 Comments
Guillaume
Guillaume on 16 May 2019
Edited: Guillaume on 16 May 2019
Ah, ok. You can use a simple 2d convolution to find the number of neighbours a value. Then it's a simple matter of a bit of arithmetic:
A = [0 0 0 0 0; 0 0 0 0 0;0 0 1 0 0; 0 0 0 0 0; 0 0 0 0 0]
nstep = 5;
for step = 1:nstep
A(A > 0) = A(A > 0) + 1; %increase existing values by 1
A = A + (conv2(A > 0, [0 1 0;1 0 1;0 1 0], 'same') == 1) .* ~A %find zeros with just one neighbour, set them to 1
end
Michael Rowlinson
Michael Rowlinson on 16 May 2019
That looks about right, I can't check if it'll work for me right now but I'll get back to this soon, thank you!

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!