我如何在MATLAB中编码一些元素?

时间:2017-11-11 08:25:51

标签: matlab split binary encode

我从0到3做了两个随机数。

a=0;
b=3;
A=round(a+(b-a)*rand(1,1000));
B=round(a+(b-a)*rand(1,1000));

然后我添加它们的每两位。然后我把它转换成二进制。

SUM =  A + B;
binarySum = dec2bin(SUM); 

因为我想计算过渡,我写这段代码:

s = 1;
for i = 1:1000
    for j = 1:3
        M(1,s) = binarySum(i,j);
        s = s+1;
    end
end
Tr = sum(diff(M)~=0);

现在我想分割M的每3个元素并用另一个元素对它们进行编码。例如000 By 000000,110 By 000001,001 By 00001,100 by 0001,101 By 001,010 By 01,011 By 1.

我使用过这种方法,但它不起作用。这有什么问题?

Lookup_In  = [  000      110      001    100    101  010  011 ] ;
Lookup_Out = {'000000','000001','00001','0001','101','01','1' } ;
StrOut = repmat({'Unknown'},size(M)) ;
[tf, idx] =ismember(M, Lookup_In) ;
StrOut(tf) = Lookup_Out(idx(tf))

1 个答案:

答案 0 :(得分:2)

M是一个可以使用Lookup_Out以这种方式映射的字符串:

M2 = reshape(M, [3,1000] )'; 

Lookup_In  = [  000      110      001    100    101  010  011 ] ;
Lookup_Out = {'000000','000001','00001','0001','101','01','1' } ;
StrOut = repmat({''},[1,size(M,1)]);

for r=1:size(M2,1)
    StrOut{r} = Lookup_Out{str2double(M2(r,:)) == Lookup_In};
end