0

I am trying to convert integer x (0<=x<=3999) to Roman numeral y.

I wrote codes for this, but I keep getting an error when I run. What is a problem of this code?

C1=['','M','MM','MMM'];
C2=['','C','CC','CCC','D','DC','DCC','DCCC','CM'];
C3=['','X','XX','XXX','XL','L','LX','LXX','LXXX','XC'];
C4=['','I','II','IV','V','VI','VII','VIII','IX'];

x=0;
for i4=1:4;
    for i3=1:9;
        for i2=1:9;
            for i1=1:9;
                if x==0
                    y='';
                else
                    y=[C1{i4} C2{i3} C3{i2} C4{i1}];
                    x=x+1;
                end
            end
        end
    end
end
Amro
  • 123,847
  • 25
  • 243
  • 454
Grace
  • 7
  • 3
  • What error do you get? Did you read it? Matlab errors are very useful if you read them carefully. – David Nov 06 '14 at 05:04
  • I got this message... Your output y = (blank) Expected output y_cor =VII – Grace Nov 06 '14 at 05:14
  • I don't see a `y_cor` variable in your code. Also, your code doesn't even run for me, so something else is wrong. – David Nov 06 '14 at 05:26
  • Sorry, a y_cor variable is in a test suite which is...x=7; y_cor='VII'; y=romanNumeral(x); assert( strcmp(y,y_cor) , ... [ '\nYour output \ny =' y '\n\n' ... 'Expected output \ny_cor =' y_cor '\n' ], ... y,y_cor); – Grace Nov 06 '14 at 05:29
  • @Grace If the test suite looks like that, you'll probably need to convert your code to a function called `romanNumeral` first. – Joachim Isaksson Nov 06 '14 at 05:33
  • My bad. I forgot to write function [y] = romanNumeral(x) in a question box. – Grace Nov 06 '14 at 05:39

2 Answers2

1

Based on this post, here is a MATLAB version:

function str = num2roman(x)
    assert(isscalar(x) && floor(x)==x);
    assert(1 <= x && x <= 3999);

    numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
    letters = {'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'};

    str = '';
    num = x;
    for i=1:numel(numbers)
        while (num >= numbers(i))
            str = [str letters{i}];
            num = num - numbers(i);
        end
    end
end

Here is the whole range of numbers converted to roman numerals:

>> x = (1:3999).';
>> xx = arrayfun(@num2roman, x, 'UniformOutput',false);
>> table(x, xx, 'VariableNames',{'integer','roman_numeral'})
ans = 
    integer      roman_numeral  
    _______    _________________
       1       'I'              
       2       'II'             
       3       'III'            
       4       'IV'             
       5       'V'              
       6       'VI'             
       7       'VII'            
       8       'VIII'           
       9       'IX'             
      10       'X'       
       .
       .
Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
0

This probably won't be helpful for your program, but it might be useful for testing/validating your results. In Matlab R2012b, and later, MuPAD in Symbolic Math toolbox can handle Roman numerals with the output::roman function. One way you can use this from within Matlab is via the following vectorized anonymous function:

toRoman @(n)feval(symengine,'map',n,'output::roman');

Then numerals = toRoman((1:12).') returns:

   I
  II
 III
  IV
   V
  VI
 VII
VIII
  IX
   X
  XI
 XII

The output is class sym. If want to convert to a cell array of strings, you can use the char function. Unfortunately, for anything but scalar Roman numerals this doesn't work by itself. You'll need to use arrayfun or a for loop:

arrayfun(@char,numerals,'UniformOuput',false)
horchler
  • 18,384
  • 4
  • 37
  • 73