why is this function running infinitely?

8 visualizzazioni (ultimi 30 giorni)
I am trying to create an eye matrix that spreads until all the inside numbers inside the border of zeros are ones, I did this using loops and it works it just never stops even after the sum of the entire matrix equals 400
Master1 = eye(22,22);
Master1(1, :) = zeros(1, 22);
Master1(22, :) = zeros(1, 22);
scan=[3:3]
temp=Master1
i=0;
while sum(sum(Master1))<400
for r=2:21
for c=2:21
scan=((r-1):(r+1):(c-1):(c+1))
if sum(scan)<1
temp(r,c)=1
else sum(scan)>1
temp(r,c)=1
end
end
end
end
Master1=temp
i=i+1
  1 Commento
Jan
Jan il 29 Nov 2021
A simplification of:
Master1(1, :) = zeros(1, 22);
Master1(22, :) = zeros(1, 22);
scan=[3:3]
is
Master1(1, :) = 0;
Master1(22, :) = 0;
scan = 3;

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 29 Nov 2021
Modificato: Image Analyst il 29 Nov 2021
You never change Master1 inside the loop so the sum of it never changes and the loop continues forever.
Not sure what you're thinking with this weird syntax:
scan=((r-1):(r+1):(c-1):(c+1));
Maybe you mean
% Extract 3x3 sub-array:
scan = temp((r-1):(r+1), (c-1):(c+1));
but here is an improvement:
Master1 = eye(22,22);
Master1(1, :) = zeros(1, 22);
Master1(22, :) = zeros(1, 22);
scan=[3:3]
temp=Master1;
loopCounter=0;
maxIterations = 100000; % Failsafe
while sum(temp(:)) < 400 && loopCounter < maxIterations
for r=2:21
for c=2:21
% Define scan with some bizarre 4 element syntax:
scan=((r-1):(r+1):(c-1):(c+1));
if sum(scan) < 1
temp(r,c) = 1;
else
temp(r,c) = 1;
end
end
end
loopCounter=loopCounter+1;
fprintf('After %d iterations, then sum of temp = %d.\n', loopCounter, sum(temp(:)));
end
Master1=temp;
fprintf('Done after %d iterations with sum of temp = %d.\n', loopCounter, sum(temp(:)))
  2 Commenti
Tyler Franks
Tyler Franks il 29 Nov 2021
Thank you, which loop should I include it under, as in after which end statement does the sum go?
Image Analyst
Image Analyst il 29 Nov 2021
@Tyler Franks The sum() is computed as it's encountered. You don't need to compute the sum and assign it to a variable.

Accedi per commentare.

Più risposte (1)

Jan
Jan il 29 Nov 2021
Modificato: Jan il 29 Nov 2021
% Simplified version:
Master1 = eye(22,22);
Master1(1, 1) = 0;
Master1(22, 22) = 0;
scan = 3;
temp = Master1;
i = 0;
while sum(Master1(:)) < 400
for r = 2:21
for c = 2:21
scan = ((r-1):(r+1):(c-1):(c+1)); % ???
if sum(scan) ~= 1
temp(r,c)=1
end
end
end
end
Master1 = temp;
i = i+1;
This will most likely not do, what you expect:
(r-1):(r+1):(c-1):(c+1)
The colon operator is defined with two : only: a:b:c is the vector from a to c in steps of b. This replies a vector. In the next step this vector is used as 1st input for the next colon operator:
((r-1):(r+1):(c-1)) : (c+1)
% ^ evaluated at first ^ then this follows
If a vector is used as input of the colon operator, only the first element is considered and the rest is ignored. Therefore your code is equivalent to:
% (r-1):(r+1):(c-1):(c+1)
(r-1):(c+1)
What is the purpose of the variable scan? Do you want to collect indices or the values of the matrix?
The problem causing the infinite loop, is that the body of the loop does not change the condition. If sum(Master1(:)) < 400 is true initially, this will never change, because Master1 is not modified inside the loop.
Why do you use the copy temp of Master1 and not the data directly? Maybe you want:
M = eye(22,22);
M(1, 1) = 0;
M(22, 22) = 0;
i = 0;
while sum(M(:)) < 400
for r = 2:21
for c = 2:21
scan = M((r-1):(r+1), (c-1):(c+1));
if sum(scan(:)) ~= 1
M(r,c) = 1;
end
end
end
M
i = i+1;
end
i

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by