Breaking a Number into 1000s, 100, 50, 10, 5,1

조회 수: 1 (최근 30일)
Ntandoyakhe Tshuma
Ntandoyakhe Tshuma 2021년 5월 25일
답변: Ntandoyakhe Tshuma 2021년 5월 25일
Hello Everyone. I am trying to figure out a way to automatically break a given number into sums of 1000, 500, 100, 50, 10, 5 and 1. For example given 1994, I want to break it into 1000 + 500 + 100 + 100 + 100 + 100 +50+ 10 + 10 + 10 +10 + 4. Or given 75 it would be 50 + 10 + 10 + 5. I would appreciate any insight on how to do this. I am trying to use this to create a function that converts a given value into Roman numerals.
  댓글 수: 2
DGM
DGM 2021년 5월 25일
There are a number of user-submitted tools on the File Exchange that can do conversion between various number systems. You could look at a few and see what approaches the various authors use.
Ntandoyakhe Tshuma
Ntandoyakhe Tshuma 2021년 5월 25일
Thank you I will have a look at them.

댓글을 달려면 로그인하십시오.

채택된 답변

Ntandoyakhe Tshuma
Ntandoyakhe Tshuma 2021년 5월 25일
**I figured it out, my solution was big in size but it worked.

추가 답변 (1개)

Jan
Jan 2021년 5월 25일
This sounds like a homework question. So please show us, what you have tried so far and ask a specific question concerning Matlab.
n = 1994;
pool = [1000, 500, 100, 50, 10, 5, 1];
v = zeros(size(pool));
for k = 1:numel(pool)
v(k) = ? % How many multiples of pool(k) are matching into n?
n = ? % Reduce n by number of found multiples
end
  댓글 수: 1
Ntandoyakhe Tshuma
Ntandoyakhe Tshuma 2021년 5월 25일
편집: Rik 2021년 5월 25일
I am working on a matlab cody problem. I have linked it here below. I am trying to create a function that converts a given number into roman numerals. I first created a function that converts roman numerals to digit form (see sample of my code below) . What l am working on is the reverse of that. It is not a homework problem l am just trying to advance my matlab skill by practicising.
I=1;
V=5;
X=10;
L=50;
C=100;
D=500;
M=1000;
%% make an array rnum
N=4;
rnum=zeros(1,N);
%% Initialize sumations
sum1=0;
sum2=0;
%% Ask the user to input their roman numeral
for ix=1:N
rnum(ix)=input(['Enter value ',num2str(ix),' of your roman numeral: '],'s');
if rnum(ix)=='I'
rnum(ix)=1;
elseif rnum(ix)=='V'
rnum(ix)=5;
elseif rnum(ix)=='X'
rnum(ix)=10;
elseif rnum(ix)=='L'
rnum(ix)=50;
elseif rnum(ix)=='C'
rnum(ix)=100;
elseif rnum(ix)=='D'
rnum(ix)=500;
elseif rnum(ix)=='M'
rnum(ix)=1000;
else
disp('Please Follow The Guide')
break
end
end
disp(num2str(rnum))
%% Compute the morden numerical value
if rnum(1)>rnum(2)
sum1=rnum(1)+rnum(2);
elseif rnum(1)==rnum(2)
sum1=rnum(1)+rnum(2);
elseif rnum(1)<rnum(2)
sum1=rnum(2)-rnum(1);
end
if rnum(3)>rnum(4)
sum2=rnum(3)+rnum(4);
elseif rnum(3)==rnum(4)
sum1=rnum(1)+rnum(2);
elseif rnum(3)<rnum(4)
sum2=rnum(4)-rnum(3);
end
disp(['Your value in morden numbers is',num2str(sum1+sum2)])

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by