function is not working

조회 수: 2 (최근 30일)
lina al noaimi
lina al noaimi 2022년 2월 22일
편집: Stephen23 2022년 2월 22일
this is my function
but im having truoble wiith the function
%% inputs from user
old_num = input('Enter the old number');
old_base = input('Enter the old base');
new_base = input('Enter the new base');
%% function to convert from one base to another
function [ b ] = base2base( a, base_from, base_to )
M = base2dec(a, base_from);
n = floor(log10(M) / log10(base_to));
b = zeros(1, n+1);
for i = 0:n
b(n + 1 - i) = mod(floor(M / (base_to^i)), base_to);
end
%% Display result
result = base2base(old_num, old_base, new_base);
fprintf('%i\n', result)

답변 (1개)

Stephen23
Stephen23 2022년 2월 22일
편집: Stephen23 2022년 2월 22일
old_num = '17';
old_base = 8;
new_base = 7;
result = base2base(old_num, old_base, new_base);
fprintf('%i\n', result)
2 1
%% !!!!!!! The function must be defined AFTER all other code !!!!!!!!!
function [ b ] = base2base( a, base_from, base_to )
M = base2dec(a, base_from);
n = floor(log10(M) / log10(base_to));
b = zeros(1, n+1);
for i = 0:n
b(n + 1 - i) = mod(floor(M / (base_to^i)), base_to);
end
end % <-------- you need END at the end of the function !!!!!!!!

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by