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

1 Ansicht (letzte 30 Tage)
Ntandoyakhe Tshuma
Ntandoyakhe Tshuma am 25 Mai 2021
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 Kommentare
DGM
DGM am 25 Mai 2021
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 am 25 Mai 2021
Thank you I will have a look at them.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ntandoyakhe Tshuma
Ntandoyakhe Tshuma am 25 Mai 2021
**I figured it out, my solution was big in size but it worked.

Weitere Antworten (1)

Jan
Jan am 25 Mai 2021
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 Kommentar
Ntandoyakhe Tshuma
Ntandoyakhe Tshuma am 25 Mai 2021
Bearbeitet: Rik am 25 Mai 2021
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)])

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by