Alternative method to dec2bin function
古いコメントを表示
I need help in the the code with alternative to bin2dec function which gives output in the double class
I have been trying but no success. Can anyone help
6 件のコメント
Jan
2022 年 3 月 7 日
This sounds like a homework question. Then please post, what you have tried so far and ask a specific question.
Hint: To get the first bit, use:
x = 17;
rem(x, 2)
To get the others, you can divide by 2 successively and round.
Haseeb Hashim
2022 年 3 月 7 日
Haseeb Hashim
2022 年 3 月 7 日
編集済み: Haseeb Hashim
2022 年 3 月 7 日
Jan
2022 年 3 月 7 日
This is the floor() function. Another hint: You do not need loops:
x = 17;
y = x ./ [1, 2, 4, 8, 16]
With floor() you get the natural numbers and rem() extracts the first bit. You can express [1,2,4,8] as pow2(0:3).
Haseeb Hashim
2022 年 3 月 7 日
編集済み: Rik
2022 年 3 月 7 日
Rik
2022 年 3 月 7 日
If you want to improve this code: try to determine the number of bits you need. That way you can use array operations. That removes the need for a loop and prevents dynamically growing your bin array.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!