how to replace char in array with a double

24 ビュー (過去 30 日間)
Seamus Herriman
Seamus Herriman 2016 年 8 月 9 日
編集済み: Stephen23 2025 年 7 月 21 日
I am making a program that converts Roman numerals to numerical values. My current code is below.
%%Roman Numeral Conversion
%Declare variables
I=1;
V=5;
X=10;
L=50;
C=100;
D=500;
M=1000;
%input Roman numeral
Roman=input('Roman numeral: ','s')
letters=cellstr(Roman')'
for n=1:length(letters)
if (strcmp(letters(n),'I'))||(strcmp(letters(n),'i'))
letters(n)=1;
elseif (strcmp(letters(n),'V'))||(strcmp(letters(n),'v'))
letters(n)=5;
elseif (strcmp(letters(n),'X'))||(strcmp(letters(n),'x'))
letters(n)=10;
elseif (strcmp(letters(n),'L'))||(strcmp(letters(n),'l'))
letters(n)=50;
elseif (strcmp(letters(n),'C'))||(strcmp(letters(n),'c'))
letters(n)=100;
elseif (strcmp(letters(n),'D'))||(strcmp(letters(n),'d'))
letters(n)=500;
elseif (strcmp(letters(n),'M'))||(strcmp(letters(n),'m'))
letters(n)=1000;
else disp('Not valid entries')
break
end
end
number=sum(letters)
text=sprintf(Roman,'= %d',number)
disp(text)
When I run this, I get the error below when I try to substitute the letter for its corresponding numerical value.
Conversion to cell from double is not possible.
Error in Roman_Numeral_Conversion (line 21)
letters(n)=10;
How can I avoid this problem?
  1 件のコメント
Stephen23
Stephen23 2025 年 7 月 20 日
Note that you can replace two STRCMP calls:
strcmp(letters(n),'V'))||strcmp(letters(n),'v')
with one STRCMPI call:
strcmpi(letters(n),'V')

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

採用された回答

James Tursa
James Tursa 2016 年 8 月 9 日
You made a cell array with this line:
letters = cellstr(Roman')'
That is why you are getting the conversion error. Make it a double instead. E.g.,
letters = zeros(size(Roman));
Then in your loop, compare with Roman instead of letters. E.g.,
if (strcmp(Roman(n),'I'))||(strcmp(Roman(n),'i'))
letters(n)=1;
elseif (strcmp(Roman(n),'V'))||(strcmp(Roman(n),'v'))
letters(n)=5;
:
etc
However, this is just a character conversion code. You still need to put in logic for when values are added vs subtracted. E.g., your code will give an answer of 11 for 'XI' as well as 'IX' when the latter should be 9.
  1 件のコメント
Seamus Herriman
Seamus Herriman 2016 年 8 月 9 日
Thanks! Yea, first I'm making a program that simply adds the Roman Numerals up. After I have that working, I'll proceed to adding logic for order.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2025 年 7 月 20 日
移動済み: Stephen23 2025 年 7 月 21 日
Another way to do this would be to set up a vector and use indexing. Let's try it with a valid and an invalid Roman numeral string.
decode('MCMLXXXVI')
ans = 1×9
1000 100 1000 50 10 10 10 5 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
decode('DIMS')
Error using solution>decode (line 10)
Roman numeral string 'DIMS' contained invalid characters
function arabic = decode(roman)
values = NaN(1, double('z'));
values('ivxlcdm') = [1, 5, 10, 50, 100, 500, 1000];
roman = char(roman);
arabic = NaN(size(roman));
arabic = values(lower(roman));
if anynan(arabic)
error("Roman numeral string '" + roman + "' contained invalid characters")
end
end
  1 件のコメント
Stephen23
Stephen23 2025 年 7 月 21 日
編集済み: Stephen23 2025 年 7 月 21 日
+1
The second NaN preallocation can be skipped, the indexing itself will generate the same vector:
decode('MCMLXXXVI')
ans = 1×9
1000 100 1000 50 10 10 10 5 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
decode('DIMS')
Error using assert
Roman numeral string contained invalid characters

Error in solution>decode (line 7)
assert(~anynan(vec),'Roman numeral string contained invalid characters')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function vec = decode(roman)
map = NaN(1,+'Z');
map('IVXLCDM') = [1,5,10,50,100,500,1000];
vec = map(roman);
assert(~anynan(vec),'Roman numeral string contained invalid characters')
end

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by