how to make a for loop for a binary number ?
10 ビュー (過去 30 日間)
古いコメントを表示
I want to build a program that converts from binary to decimal (I know that there is command for this , I want to build it my self ) so for exmple if
x=10010
I want to press x(3) and get 0 ( just like if i have an array of numbers for exmaple x=[1 42 13 ] so x(2)=42 ) how do i do it ?
0 件のコメント
回答 (1 件)
Stephen23
2018 年 7 月 4 日
編集済み: Stephen23
2018 年 7 月 4 日
In MATLAB binary numbers are normally stored as char vectors:
>> x = '10010'
x = 10010
>> x(3)
ans = 0
>> bin2dec(x)
ans = 18
You could put them as separate elements of a numeric array, but there is little advantage to this (and it makes displaying them harder). Note that the conversion from char vector to numeric vector (and back again) is trivial:
>> y = x-'0'
y =
1 0 0 1 0
>> char(y+'0')
ans = 10010
As an alternative you could write your own class (not trivial).
1 件のコメント
Guillaume
2018 年 7 月 4 日
編集済み: Guillaume
2018 年 7 月 4 日
Storing the bits as 0/1 digits of a numeric array has the slight advantage that you can apply OR, XOR, AND and CMP operations (using |, xor, & and ~ respectively, not the bitxxx operators), as long as the bits are aligned of course.
Arithmetics (addition, subtraction, etc.) is still out with either model.
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!