MATLAB integer to roman numeral

9 ビュー (過去 30 日間)
Jacob Willett
Jacob Willett 2018 年 10 月 31 日
回答済み: Rik 2018 年 10 月 31 日
I am working on a lab that requires me to convert integers to roman numerals.
I have the following code:
function r = num2rom(x)
d = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
c = {'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'};
r = [];
for i = 1:numel(d)
if x >= d(i)
z = idivide(num, d(i));
r = [r, repmat(c(i), z)];
end
end
end
But I am getting several errors, these are the errors returning:
Undefined function 'ge' for input arguments of type 'cell'.
Error in Lab4>num2rom (line 112)
if x >= d(i)
Error in Lab4 (line 76)
num = num2rom(num)
I have been scratching my head at this, any help would be appreciated.
  1 件のコメント
Rik
Rik 2018 年 10 月 31 日
There is no ge function anywhere in your question. Please make sure to quote all the code that is needed to reproduce the error, and then paste all the red text here.

サインインしてコメントする。

回答 (1 件)

Rik
Rik 2018 年 10 月 31 日
I wouldn't hard-code the combinations, but use the rule instead. My proposal would look something like this:
  1. convert the char array to a vector (using the cell you defined already)
  2. mark all values that apear before a larger value negative
  3. sum the vector elements
If you have trouble writing the code, just write a comment.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by