Recursive function simulating traversing the hypercube

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 Commenti

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...
Thanks for your response, I had the sI2siplus1 because I need to compare the value attached to the S and the S with each 0 switched to a 1 then if the value attached to the slightly altered S is greater than the previous S then a path exists between the two points. Only if theyre greater can we move to the SI = SI + 1 Basically the function is to find the number of paths from a given binary string to the string [1,1,....,1].

Accedi per commentare.

Risposte (0)

Categorie

Richiesto:

il 22 Dic 2017

Commentato:

il 22 Dic 2017

Community Treasure Hunt

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

Start Hunting!

Translated by