Is there a way to get an 8 x 1 matrix instead of 8 x 3 using dec2bin or any other?

2 次查看(过去 30 天)
dec2bin (2 ^ n-1: -1: 0) - '0'
  2 个评论
sawasawa
sawasawa 2021-2-27
Thanks for the link.
This is what I get
n=3
b=dec2bin (2 ^ n-1: -1: 0) - '0'
ans =
1 1 1
1 1 0
1 0 1
1 0 0
0 1 1
0 1 0
0 0 1
0 0 0
But I want
111
110
101
100
011
010
001
000

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-2-27
string(dec2bin (2 ^ n-1: -1: 0))
This will be considered an array. The entries will be string objects such as "011". The entries will not be numeric.
  1 个评论
Walter Roberson
Walter Roberson 2021-2-27
Perhaps you want
n=3
n = 3
b = (dec2bin (2 ^ n-1: -1: 0) - '0')*10.^(n-1:-1:0).'
b = 8×1
111 110 101 100 11 10 1 0
Note that if you do this, then the only way to get the leading zeros is to format it as text again.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2021-2-27
编辑:Jan 2021-2-27
If you want 000 as output, remember, that this is not a valid decimal value. If you need the zeros for any reason, you have to stay at the char representation. Simply omit the -'0' part:
n = 3;
b = dec2bin(2 ^ n-1:-1:0)
The cannot be a 8x1 matrix.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by