More time to run with higher input value ( more than 100 or 200)
2 views (last 30 days)
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
Accepted Answer
Stephen23
on 30 Mar 2015
Edited: Stephen23
on 30 Mar 2015
You need to avoid using loops, and learn to vectorize your code. This would be simplest and most reliable way of making this code faster.
You should have a look at MATLAB's own advice on improving performance. Here are some general pieces of advice about writing fast code in MATLAB, some of which you might find useful:
- use vectorized code rather than loops.
- preallocate arrays before loops.
- use logical indexing rather than subscripts.
- do not change a variable's data class.
- do not clear unnecessarily.
- use tic and toc to time parts of a program.
- use the profiler to check how long all parts of a function require.
Additional Tips on Improving Performance
If the performance of your program remains a concern, then consider the following suggestions:
- Split large script files into smaller ones, having the first file call the second if necessary.
- Construct separate functions (or local functions and nested functions) from the larger chunks of code.
- If you have overly complicated functions or expressions, use simpler ones to reduce size. * Simpler functions often make good utility functions that you can share with others.
- Use functions instead of scripts because they are generally faster.
- Vectorize your code to take advantage of the full functionality of MATLAB.
- Use a sparse matrix structure to vectorize code without requiring large amounts of memory.
- Avoid running large processes in the background while executing your program in MATLAB.
- Avoid overloading MATLAB built-in functions on any standard MATLAB data classes because this can negatively affect performance.
3 Comments
Stephen23
on 31 Mar 2015
Edited: Stephen23
on 31 Mar 2015
I really have no idea why you have accepted this answer, as your query does not seem to be resolved. In any case, your comment with the code "correction" makes no sense:
if y=0:1
if y=0:1, disp(y), end
|
Error: The expression to the left of the equals sign is not a valid target for an assignment.
Even if we change the assignment operator to be a logical equivalence, then assuming that y is a scalar, this if case would never be true, thus the code inside would never execute:
if y==0:1 % <- will never be true
Read the if documentation to know why.
As broken code tells nothing useful, there is not much more I can do right now. Please describe the actual problem that you are trying to solve.
More Answers (1)
Stephen23
on 31 Mar 2015
Edited: Stephen23
on 31 Mar 2015
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.
See Also
Categories
Find more on Introduction to Installation and Licensing 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!