2017-11-11 178 views
1

我做了两个随机数从0到3我怎么能编码MATLAB中的一些元素?

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到000000,110通过000001,001通过00001,100通过0001,101 001,010通过01,011由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)) 

回答

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 
相关问题