Recursive function simulating traversing the hypercube
Show older comments
I'm fairly new to matlab and Im trying to write a function that considers every vertex in a hypercube and assigns it a random number between 0 and 1. If one vertex is identical to another vertex apart from one entry then a path exists if the random number is greater on one than the other. The value m is a random matrix. I know that doesnt make much sense but hopefully it'll become clear looking at my function.
function[nopaths] = paths(S,m,nopaths)
m
n=(log2(length(m)));
if S==ones(1,n)
nopaths = nopaths + 1;
else
for i = 1:n
if S(i) == 0
q = bi2de(S);
p = SI2SIplus1(S,i);
p1 = bi2de(p);
if m(q+1) < m(p1+1)
S(i) = S(i) + 1;
paths(S,m,nopaths)
end
end
end
end
end
In my function the S12SIplus1 is a function that takes the ith value in a matrix and adds 1. However my function doesn't do every S as it changes the S each time and I dont know how to change that. Thanks very much for any help
2 Comments
Geoff Hayes
on 22 Dec 2017
Alden - can you clarify your statement However my function doesn't do every S as it changes the S each time and I dont know how to change that? Also, if S12SIplus1 is a function that takes the ith value in a matrix and adds 1 then why do you have the line of code
S(i) = S(i) + 1;
Do you do this twice then?
And...since paths returns a parameter, then I suspect you want to change your line
paths(S,m,nopaths);
to
nopaths = paths(S,m,nopaths);
or set the return to some other variable...
Alden Robertson
on 22 Dec 2017
Answers (0)
Categories
Find more on File Operations 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!