I need help on how to fix this code. It gives me an error \"Out of memory. The l
ID: 3596691 • Letter: I
Question
I need help on how to fix this code. It gives me an error "Out of memory. The likely cause is an infinite recursion within the program.
it gives me an error ."
function [ filled ] = travelDistance( blank )
%TRAVELDISTANCE
% blank: two-dimensional array comprised of -1s, 0s, and 1s
% filled: blank that is modified (replace every 0 in blank with its
% distance to the nearest 1, starting at 2, traveling along cardinal
% directions without passing through a -1 value)
filled=blank;
[a,b]=size(blank);
for f=1:1000
for x=2:a
for y=2:b
filled(x,y);
if filled(x,y)==0
if (filled(x-1,y)==f||filled(x+1,y)==f||filled(x,y-1)==f||filled(x,y+1)==f)
filled(x,y)=helper(f,1,f,x,y);
end
else
filled(x,y);
end
end
end
end
end
function filled = helper(initial,increment,final,x,y)
filled=helper(initial+increment,increment,final,x,y);
end
Explanation / Answer
function [ filled ] = travelDistance( blank )
%TRAVELDISTANCE
% blank: two-dimensional array comprised of -1s, 0s, and 1s
% filled: blank that is modified (replace every 0 in blank with its
% distance to the nearest 1, tarting at 2, traveling along cardinal
% directions without passing through a -1 value)
filled=blank;
[a,b]=size(blank);
for f=1:infinity
for x=2:a
for y=2:b
filled(x,y);
if filled(x,y)==0
if (filled(x-1,y)==f||filled(x+1,y)==f||filled(x,y-1)==f||filled(x,y+1)==f)
filled(x,y)=helper(f,1,f,x,y);
end
else
filled(x,y);
end
end
end
end
end
function filled = helper(initial,increment,final,x,y)
filled=helper(initial+increment,increment,final,x,y);
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.