生成比特矩阵 [英] Generate matrix of bits
问题描述
我想借此整数 N
确定的位数在我的沟通code和矢量定义,我分配给位字母 0:N-1
,并输出包含字母符号每个状态矩阵/单元阵列,例如:
I would like to take an integer n
defining the number of bits in my communication code and a vector defining the alphabet I am assigning to bits 0:n-1
, and output a matrix/cell array containing the alphabetic notation for each state, i.e.:
function M = mycommarray(3,[-1,1])
产生
M = [{-1,-1,-1}, {-1,-1,1}...]
我试着用这样一个简单的方法DEC2BIN(0:7,3)
,但似乎没有成为一个快速的方法,使零到 1
秒。
I tried doing this an easier way with dec2bin(0:7,3)
, but there doesn't seem to be a quick way to make the zeros into -1
s.
有任何接近prepackaged做这个?在这种情况下,我不希望任何人,使之成为我(与家庭作业)。
Is there anything close to prepackaged that does this? In this case I don't want anyone to make it for me (related to homework).
推荐答案
DEC2BIN
其实并不是来解决这个问题,因为它的结果是一个字符串数组的最佳方式(即的字符的矩阵),其中每个数字是一个字符。如果你想 - 1。
重新present逻辑0,这将是两个字符,它引入的问题。
dec2bin
is actually not the best way to approach the problem, because its result is an array of strings (i.e a matrix of characters), where each digit is a character. If you want '-1'
to represent a logical "0", that would be two characters, and it introduces problems.
考虑使用 bitget
的替代方法。利用夏嘉曦的建议的,请执行以下操作:
Consider an alternative method with bitget
. Making use of Shai's suggestion, do the following:
[bits, values] = meshgrid(1:3, 0:7);
M = 2 * bitget(values, bits) - 1;
本会产生你想要什么:
The will produce what you want:
M =
-1 -1 -1
1 -1 -1
-1 1 -1
1 1 -1
-1 -1 1
1 -1 1
-1 1 1
1 1 1
这篇关于生成比特矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!