More time to run with higher input value ( more than 100 or 200)

2 views (last 30 days)
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
soma biswas
soma biswas on 31 Mar 2015
>> graph(20)
0 1/20 1/19 1/18 1/17
Columns 6 through 10
1/16 1/15 1/14 1/13 1/12
Columns 11 through 15
1/11 1/10 2/19 1/9 2/17
Columns 16 through 20
1/8 2/15 1/7 3/20 2/13
Columns 21 through 25
3/19 1/6 3/17 2/11 3/16
Columns 26 through 30
1/5 4/19 3/14 2/9 3/13
Columns 31 through 35
4/17 1/4 5/19 4/15 3/11
Columns 36 through 40
5/18 2/7 5/17 3/10 4/13
Columns 41 through 45
5/16 6/19 1/3 7/20 6/17
Columns 46 through 50
5/14 4/11 7/19 3/8 5/13
Columns 51 through 55
7/18 2/5 7/17 5/12 8/19
Columns 56 through 60
3/7 7/16 4/9 9/20 5/11
Columns 61 through 65
6/13 7/15 8/17 9/19 1/2
Columns 66 through 70
10/19 9/17 8/15 7/13 6/11
Columns 71 through 75
11/20 5/9 9/16 4/7 11/19
Columns 76 through 80
7/12 10/17 3/5 11/18 8/13
Columns 81 through 85
5/8 12/19 7/11 9/14 11/17
Columns 86 through 90
13/20 2/3 13/19 11/16 9/13
Columns 91 through 95
7/10 12/17 5/7 13/18 8/11
Columns 96 through 100
11/15 14/19 3/4 13/17 10/13
Columns 101 through 105
7/9 11/14 15/19 4/5 13/16
Columns 106 through 110
9/11 14/17 5/6 16/19 11/13
Columns 111 through 115
17/20 6/7 13/15 7/8 15/17
Columns 116 through 120
8/9 17/19 9/10 10/11 11/12
Columns 121 through 125
12/13 13/14 14/15 15/16 16/17
Columns 126 through 129
17/18 18/19 19/20 1
soma biswas
soma biswas on 31 Mar 2015
Edited: soma biswas on 31 Mar 2015
en.wikipedia.org/wiki/Farey_sequence
I am doing a project on radar interception. Where I have to find the interception time using farey sequence method.
That code is for farey sequence. I have written that code. I need higher order values (ex: 100 or 200 +) particular node value i need because each node output is very large.
The Farey series have the following properties: Let h/k and h'/k' be two adjacent elements of the series. Their median is defined as (h +h')/(k+k)'and when the order reaches k+k' it is added to the series between h/k and h'/k' . Moreover, the elements of the Farey series also obey unimodularity property, i.e. h'k −hk'=1.Then, the Farey series can be used to determine intercept time as follows
1. h/k and h/k' are the adjacent values of α (so alpha is median of h/k and h/k')and ε is the order of farey seires.
2. h k ≤ α ≤ h' k'
3. By using farey seires I need h/k and h/k’ so that I use these two value to find interception point.

Sign in to comment.

Accepted Answer

Stephen23
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.
In the MATLAB documentation we also find these tips:
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
soma biswas
soma biswas on 31 Mar 2015
I tried in other way as you said and still trying but i couldn't get output. With this method i am getting but problem with higher value or 'm'
Stephen23
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
because it is an assignment operator being used as an if conditional, which produces an error:
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.

Sign in to comment.

More Answers (1)

Stephen23
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.

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!