Decimals to Roman Numerals

9 ビュー (過去 30 日間)
Michael  Kurniawan
Michael Kurniawan 2012 年 10 月 3 日
Hello I'm student currently studying MATLAB and I have a project to turn Decimals to Roman Numerals. I wanted to know if it was possible to make matlab recognize individual digits. eg. 1000 x1=1 x2=0 x3=0 x4=0
as then my code will follow into:
for number > 1000 if x1=1 roman= 'M' if x1= 2 roman='MM'
any help would be greatly appreciated thanks.

採用された回答

José-Luis
José-Luis 2012 年 10 月 3 日
a = 1000;
a = num2str(a);
a = a - '0';
a is a row vector, where each element is one of your x's
  2 件のコメント
Michael  Kurniawan
Michael Kurniawan 2012 年 10 月 3 日
I understand how this works but what if I typed a number such 900 would this put the vector [ 9,0,0 ] and so I have to type 0900 for this method to work.
sorry for forgetting to mention but my code is for 0-3000 Thank you again
José-Luis
José-Luis 2012 年 10 月 3 日
If you want four digits, you could, for instance:
your_vals = repmat('0',1,4);
a = 900;
a = num2str(a);
your_vals(end-numel(a)+1:end) = a;
your_vals = your_vals - '0';

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2012 年 10 月 3 日
from Cody
function ans = dec2rom(z)
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'};
[];
for ii = 1:numel(d)
if z >= d(ii)
ans = [ans,repmat(c{ii},1,fix(z/d(ii)))];
z = rem(z,d(ii));
end
end
% eg
>> dec2rom(2012)
ans =
MMXII
  6 件のコメント
Michael  Kurniawan
Michael Kurniawan 2012 年 10 月 3 日
Thank you to all of you for the help, I'm a bit slow in understanding codes so in the future I might ask some very silly questions and I hope you all will still help me and once i again thank you
José-Luis
José-Luis 2012 年 10 月 3 日
Please accept an answer if it helped you.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by