More time to run with higher input value ( more than 100 or 200)
Show older comments
Its taking lots of time to run the code for higher values for "m".please help
function graph(m)
F=0;
format rat
for i=1:m
for r=0:i
for s=0:i
for x=0:i
for y=0:i
if r*y-s*x==1||r*y-s*x==-1
if r/s<=1
if x/y<=1
F=union(F,r/s);
F=union(F,x/y);
end
end
end
end
end
end
end
end
disp(F)
end
6 Comments
Stephen23
on 30 Mar 2015
Every time I run this code it displays the same output: 0. What is the desired output? Can you give some specific examples? If you give specific examples, then we can help you write vectorized code that would achieve what you want.
soma biswas
on 31 Mar 2015
soma biswas
on 31 Mar 2015
Your "output example" is an image of a directed graph. Can you please tell us which values you want: all nodes, or just the highlighted ones? From all levels, or just from one level? Can you please provide a reference or more examples? I would like to test some new code, but this is not adequate to know what the algorithm is.
soma biswas
on 31 Mar 2015
soma biswas
on 31 Mar 2015
Edited: soma biswas
on 31 Mar 2015
Accepted Answer
More Answers (1)
The Farey Sequence would be best generated using some fully vectorized code. The following generates the numerator and denominator for any level of the Sequence (i.e. F4, etc.), including repetitions, and excluding the zero and one terms:
>> lvl = 5;
>> vec = 1:(lvl*(lvl-1))/2;
>> num = vec + round(sqrt(2*vec))/2 - (round(sqrt(2*vec)).^2)/2
num =
1 1 2 1 2 3 1 2 3 4
>> den = 1 + round(sqrt(2*vec))
den =
2 3 3 4 4 4 5 5 5 5
which means you can easily generate the whole Farey Sequence like this:
>> [0,unique(num./den),1]
ans =
0 0.2 0.25 0.3333 0.4 0.5 0.6 0.6667 0.75 0.8 1
This will be many many times faster and much more robust than trying to solve this using loops. Please learn to write vectorized code in MATLAB.
1 Comment
soma biswas
on 31 Mar 2015
Categories
Find more on Nearest Neighbors 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!