In: Computer Science
I'm new in MATLAB and I have to write a code in MATLAB which converts a number from one base to another without using base2base, etc
So i am taking an example.
we will Write code to convert a number from any base to decimal. The input (number to be converted) is represented as a vector of size n where n represents number of digits.
MATLAB CODE-
%ask for input number as vector
v = input('Enter the number to be converted as vector : ');
%ask for its based
b = input('Enter the base of the input number : ' );
n = length(v); %store the length of vector
c = 0; %to store the counter for power of base
d = 0; %to store the final decimal converted number
%iterate over the vector input number backwards
for i = n:-1:1
%use the formula for conversion
d = d + ((b^c)*v(i));
%increment the power to base by 1
c = c + 1;
end
%print the final decimal number
fprintf('Final decimal converted number : %d\n',d);
IMAGE OF CODE-
OUTPUT-
Case 1
Case 2
****PLEASE DON'T FORGET TO GIVE THUMBS UP. I REALLY NEED IT