5.0

5.0 | 3 ratings Rate this file 23 Downloads (last 30 days) File Size: 4.28 KB File ID: #26161
image thumbnail

ROMAN2NUM and NUM2ROMAN: modern Roman numerals

by

 

21 Dec 2009 (Updated )

Converts Roman numerals to and from integers

| Watch this File

File Information
Description

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.

MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
08 Aug 2014 Marcel Rüland  
17 Oct 2010 Forrest Brett

Impressive coding and useful functions.

22 Dec 2009 Oleg Komarov  
22 Dec 2009 François Beauducel

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

22 Dec 2009 Oleg Komarov

François,
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

num2roman(32.56)
ans =
XXXII
-- 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 =
NaN
-- i would convert 'xii' to upper inside the fcn; x = upper('xii');

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

22 Dec 2009 François Beauducel

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];

François.

22 Dec 2009 Oleg Komarov

Useful but:
1) No need to paste the license inside the files
2) No checks
3) Can be made more readable
ex:
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....

Oleg

Updates
22 Dec 2009

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

Contact us