How do you change bases without dec2base, etc.?

조회 수: 2 (최근 30일)
Jen Kirk
Jen Kirk 2017년 2월 14일
편집: Stephen23 2017년 2월 14일
I have to write a function that changes any positive real number in base 10 to any base from 2 to 9. The main part that I am struggling with is putting the inputed value into an array so you can manipulate each element. So for example, if I'm converting 23 in base 10 to base 2, how would I put the 23 into an array so that I could go about converting it? Any help would be greatly appreciated.
  댓글 수: 2
Adam
Adam 2017년 2월 14일
What is wrong with just:
a = 23;
?
Though I'd name it something more useful.
Jen Kirk
Jen Kirk 2017년 2월 14일
I need to put each element of the number, the 2 and the 3, into an array so that I can manipulate each piece individually. I was trying to use num2str but it wasn't working..

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

채택된 답변

Stephen23
Stephen23 2017년 2월 14일
편집: Stephen23 2017년 2월 14일
Method one: num2str (does not work with decimal digits):
>> N = 23;
>> vec = num2str(N)-'0'
vec =
2 3
Method two: from powers of ten:
>> pwr = floor(log10(N));
>> vec = mod(floor(N ./ 10.^(pwr:-1:0)), 10)
vec =
2 3

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by