34 views (last 30 days)
Show older comments
Tanner on 29 Apr 2024 at 3:50
Edited: Stephen23 on 29 Apr 2024 at 6:23
Open in MATLAB Online
For my recursive function, I am trying to create a hexadoku solver. One of my variables 'mm' keeps outputting a NaN even when I try to change the NaNs into -1. I also believe that there is a base I am missing in order for this function to end. I do realize that the readmatrix function will output a NaN where there isn't an integer, but that is why there is the M(isnan(M)) = -1.
M = readmatrix("puzzle_1.in","FileType","text");
M(isnan(M)) = -1;
S = zeros(size(M));
Hexadoku(M,S)
function [S] = Hexadoku(M,S)
if ~exist('S')
S = zeros(size(M));
end
FirstID = M==-1;
if isempty(FirstID)
M = S(:,:,size(S,4)+2);
else
[i,j] = ind2sub([16,16],FirstID);
for k = 1:16
ii = (ceil(i/4)-1)*4+5;
jj = (ceil(j/4)-1)*4+1;
mm = M(ii:ii+3,jj:jj+3);
mm(isnan(mm)) = -1;
if (M(i+1,:)==k)==0
if (M(:,j)==k)==0
if (mm(:)==k)==0
M(i+1,j) = k;
S = Hexadoku(M,S);
end
end
end
end
end
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Answers (1)
Stephen23 on 29 Apr 2024 at 3:54
Edited: Stephen23 on 29 Apr 2024 at 4:06
Open in MATLAB Online
Replace this (which returns logical indices):
FirstID = M==-1;
with this (which returns the linear index):
FirstID = find(M==-1,1,'first');
Note that:
- FIND can also return row&column subscript indices (i.e. you probably do not need IND2SUB).
- Using NARGIN is more efficient than EXIST.
- Your IF-statements if ...==0 will only be considered as TRUE when all elements are non-zero. This is unlikely to be the case. You probably require ANY or ALL to clarify what behavior you require, otherwise your code is very unlikely to be doing what you expect.
4 Comments Show 2 older commentsHide 2 older comments
Show 2 older commentsHide 2 older comments
Tanner on 29 Apr 2024 at 4:08
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2112761-how-do-i-fix-my-nan-issue-within-a-recursive-function#comment_3146081
Open in MATLAB Online
While this does give a different answer, that answer is a recursion error. Matlab says that the error could be cuased by:
[i,j] = ind2sub([16,16],FirstID);
I am guessing it would be because of the ind2sub function.
Stephen23 on 29 Apr 2024 at 4:15
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2112761-how-do-i-fix-my-nan-issue-within-a-recursive-function#comment_3146086
Edited: Stephen23 on 29 Apr 2024 at 4:16
"While this does give a different answer, that answer is a recursion error."
In most cases recursion errors occur because the user has not defined clear conditions to end the recursion, or they simply call their own function inside itself without understanding what that does.
"Matlab says that the error could be cuased by: I am guessing it would be because of the ind2sub function."
It would be highly unusual for IND2SUB to cause infinite recursion, unless you have defined your own IND2SUB somewhere. If you want help debugging this then please post the complete error message and the complete current code that you are running.
Tanner on 29 Apr 2024 at 5:24
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2112761-how-do-i-fix-my-nan-issue-within-a-recursive-function#comment_3146131
Open in MATLAB Online
I believe I am incorrect with how the 'ii', 'jj', and 'mm' variables work. This is the script:
M = readmatrix("puzzle_1.in","FileType","text");
M(isnan(M)) = -1;
S = zeros(size(M));
Hexadoku(M,S)
function [S,Mout] = Hexadoku(M,S)
if nargin == 1
S = zeros(size(M));
end
FirstID = find(M==-1,1,"first");
if M ~= -1
Mout = M(:,:);
else
[i,j] = find(FirstID == 1);
for k = 1:16
ii = (ceil(i/4)-1)*4+5;
jj = (ceil(j/4)-1)*4+1;
mm = M(ii:ii+3,jj:jj+3);
mm(isnan(mm)) = -1;
if all(M(i,:)==k)
if all(mm(:)==k)
M(i,j) = k;
S = Hexadoku(M,S);
end
end
end
end
end
This is the error:
Out of memory. The likely cause is an infinite recursion within the program.
Error in HexadokuTest>Hexadoku (line 23)
S = Hexadoku(M,S);
Stephen23 on 29 Apr 2024 at 5:40
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2112761-how-do-i-fix-my-nan-issue-within-a-recursive-function#comment_3146141
Edited: Stephen23 on 29 Apr 2024 at 6:23
So check your stopping conditions: are they ever fulfilled?
(When I write "check" I do not mean "rely on what you believe your code is doing", I actually mean check the recursive function calls for its input valus and the stopping conditions. For example, a good start is to print them to the command window. A much better approach is to use the debugger.)
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABProgrammingFunctionsVariables
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office