Function inside function - generating Random Walks
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jan Novotny
am 11 Mär. 2018
Kommentiert: Jan Novotny
am 11 Mär. 2018
I'm trying to create a random walk in an arbitrary number of dimensions (with the goal to estimate the number of self-avoiding walks of length n in d dimensions).
To do this I need two external functions. The first checks if any given coordinate has already been crossed during previous steps of a walk.
The second one returns a list of directions possible to walk in (since we want to use importance sampling with resampling to create a walk which is not allowed to go to a coordinate where it has already been). For example, if I have a path in d = 2 with coordinates (0,0) (0,1) (-1,1) (-1, 0), and thus the walker is now standing at point (-1, 0), the function should return a list of two directions (representing either +1/-1 steps in either x or y direction) since the only free neighbouring points would be (-2, 0) or (-1, -1).
I have named these two functions free.m and possible_dir.m respectively, and they look as follows:
function [p] = free(coord, path, n)
% Checks if a coordinate is free
% Checks if the input coordinate is in the path or not, i.e. if it has
% already been crossed by our walker. If coord is not in path, returns TRUE (=1)
if n>0
if isequal(coord, path(n,:))
p=false;
else
free(coord, path, n-1)
end
else
p=true;
end
end
and:
function [dir] = possible_dir(current_coord, path, n, dims, direction)
% Give us vector with possible directions to walk in
% Possible directions rank from 1 to 2*d, with each direction being given
% the corresponding number
dir_list = zeros(1, 2*dims);
for i=1:2*dims
if free((current_coord + direction(i,:)), path, n)
dir_list(i) = i;
else
dir_list(i) = 0;
end
end
dir = dir_list(dir_list>0);
end
Now take this 2-dimensional walk of currently three steps as an example:
test_path = zeros(3, 2);
test_path(1,:)=[0 0];
test_path(2,:)=[1 0];
test_path(3,:)=[1 1];
Inserting the coordinate (1 2) into the free-function returns TRUE, and inserting the coordinate (1 1) returns FALSE, as expected.
Now, inserting coordinate (1 1) into the possible_dir-function just gives me the error message:
Output argument "p" (and maybe others) not assigned during call to "free".
Error in possible_dir (line 7) if free((current_coord + direction(i,:)), path, n)
Error in proj3 (line 682) possible_dir(cc, test_path, 3, 2, DIR)
Why is this error occurring?
1 Kommentar
John D'Errico
am 11 Mär. 2018
How high a dimension? You could just use an n-d boolean matrix to see if you have ever passed through a point. As long as n is not too large, that will work. Make it a logical array, so it does not take much space.
The set of possible directions would then also be easy to identify, since you just check each dimension in the array.
Akzeptierte Antwort
John D'Errico
am 11 Mär. 2018
Bearbeitet: John D'Errico
am 11 Mär. 2018
As far as why that error occurs, LOOK AT YOUR CODE! READ THE ERROR MESSAGE!
"Output argument "p" (and maybe others) not assigned during call to "free"."
Your code:
if isequal(coord, path(n,:))
p=false;
else
free(coord, path, n-1)
end
Are there any circumstances where p will not be defined?
OF COURSE! What happens if the second branch is taken in that if? Is p defined?
If you call a function, even if the call is a recursive ne, and you do NT define the output variable, then p is never returned.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!