我如何在 MATLAB 中对某些元素进行编码?

How i can encode some elements in MATLAB?

我从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))

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