File Exchange

image thumbnail

ROMAN2NUM and NUM2ROMAN: modern Roman numerals

version 1.1 (4.28 KB) by

Converts Roman numerals to and from integers



View License

These two scripts convert Roman numerals to and from any integer (scalar, vector or matrix), including large numbers greater than 4999 with the parenthesis notation (multiplies by 1000).

The function NUM2ROMAN uses strict rules of modern notation (substractive principle for 4 and 9 bases) except for the common 'MMMM' form replacing '(IV)'.

ROMAN2NUM is more flexible and is able to convert some other Roman notation possibilities, for instance the 3 different expressions roman2num({'IC','XCIX','XCVIIII'}) return [99,99,99], or roman2num({'MDXV','MCCCCCXV'}) return [1515,1515].

See help for syntax, and script comments for details.

Comments and Ratings (10)

Jomel Canales


Jomel Canales

Give the code you pricking shit

An Do

An Do (view profile)

Forrest Brett

Impressive coding and useful functions.

Oleg Komarov

Oleg Komarov (view profile)

Thanks for your time Oleg.
In fact, I was thinking about checks under normal use of the functions (a user that have read the help, at least...) ! You are perfectly right, this is good practice to anticipate all possible misunderstandings / misuse of the function.
Note that fractionnary parts do exist in Roman numerals, but to my knowledge, they are used only for fractions bellow 1 (multiple of 1/12). This will be in a future update.
Thanks for having detected the lowercase problem.

Oleg Komarov

Oleg Komarov (view profile)

1) the license is automatiocally associated with your .zip file, therefore no need to paste it, but of course no damagae from that may occur.

2) " invalid forms in roman2num will return NaN". This is not always true. It is good practice though to place some checks.
I'll list some error that may occur:

>> roman2num()
??? Input argument "x" is undefined.
Error in ==> roman2num at 49
if ischar(x)
-- I would place the generale error(nargchk(1,1,nargin)) check

>> roman2num(32)
??? Cell contents reference from a non-cell array object.
Error in ==> roman2num at 56
ss = x{k};
-- I would expect only char or cellstr type are accepted:
if iscell; if ~iscellstr(..); error(..); end; elseif ~ischar(..); error(..); end

>> num2roman()
??? Input argument "n" is undefined.
Error in ==> num2roman at 48
s = cell(size(n));
-- same as roman2num

>> num2roman([32,33]); % This one works
>> num2roman({32,33})
??? Undefined function or method 'log2' for input arguments of type 'cell'.
Error in ==> log10 at 20
y = log2(x);
Error in ==> num2roman at 51
m = max(floor((log10(n(k)) - log10(5000))/3) + 1,0);
-- here another check is needed. Only numeric arrays should be supoprted
if ~isnumeric(...); error(...); end

ans =
-- not only this is philosophically wrong but it doesn't even consider approximation. I would "convert" only integer values.
you can use mod or rem to check.
According to my opinion num2roman(32.56) --> NaN.

4) roman2num('xii')
ans =
-- i would convert 'xii' to upper inside the fcn; x = upper('xii');

This is what comes into my mind...hope it helps you somehow.

Thanks for your comments Oleg.

Some answers:

1) the copy of license inside the script has been approved by the Matlab Central administrator; I thought it was mandatory with BSD source codes;

2) no check is a priori needed: num2roman converts any number, and invalid forms in roman2num will return NaN. Did you experienced a particular problem ?

3) well, I don't like hard-coding when it can be written with an equation! The reshape is needed for the next line
a(8:9) = 0;
but you are right, this is not very readable. Can be advantageously replaced by hard-coded
a = [1, 5, 10, 50, 100, 500, 1000, 0, 0];


Oleg Komarov

Oleg Komarov (view profile)

Useful but:
1) No need to paste the license inside the files
2) No checks
3) Can be made more readable
a = reshape([1;5]*10.^(0:3),1,8); % values of each Roman numerals
a = [1, 5, 10, 50, 100, 500, 1000, 5000];
No need to use reshape....




Adds some basic checks on the input arguments; ROMAN2NUM now accepts lowercase (all thanks to Oleg Komarov comments).

MATLAB Release
MATLAB 6.5 (R13)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video