Decimals to Roman Numerals
古いコメントを表示
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.
採用された回答
その他の回答 (1 件)
Andrei Bobrov
2012 年 10 月 3 日
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 件のコメント
José-Luis
2012 年 10 月 3 日
This was a homework question...
Daniel Shub
2012 年 10 月 3 日
is []; a cody "trick"? Presumably it initializes ans?
@Jose-Luis, while the given solution scores well on cody, its usefulness as a homework solution is probably limited.
José-Luis
2012 年 10 月 3 日
Fair enough
Andrei Bobrov
2012 年 10 月 3 日
Hi Daniel! Yes.
Michael Kurniawan
2012 年 10 月 3 日
José-Luis
2012 年 10 月 3 日
Please accept an answer if it helped you.
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!