1

Is there anyway of obtaining the numerator and denominator in MATLAB without using numden() function? For example: format rational x=5/2;

I want to obtain 5 as num and 2 as den. Can you help me with this tricky problem.

dmfrl
  • 337
  • 8
  • 17
  • 1
    Look at another so [post](http://stackoverflow.com/questions/95727/how-to-convert-floats-to-human-readable-fractions) – angainor Sep 22 '12 at 23:01
  • Why you do not want to use numdem? It would be much simpler, no? – Oli Sep 23 '12 at 00:11

1 Answers1

2

How about

[N,D] = rat(2.5)

Otherwise, if you insist on doing it yourself, you can do something like

N = 2.5;

D=1; while (int64(N)~=N), N=N*10; D=D*10; end

g = gcd(N,D);

D = D/g;
N = N/g;
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96