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

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

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

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

 채택된 답변

Guillaume
Guillaume 2019년 5월 15일
편집: Guillaume 2019년 5월 15일
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

I should've probably added that the pattern won't spread to a zero if it has two or more occupied spaces around it such as having a 1 or 2 and another 1 or 2 adjacent to it.
so the next step after the third would be something like this but I don't think this would be the case because it's being demonstrated on a 5x5 array and would be exceeding the boundary?
0 1 2 1 0
1 0 3 0 1
2 3 4 3 2
1 0 3 0 1
0 1 2 1 0
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
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!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2019년 5월 15일

댓글:

2019년 5월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by