2017-11-11 180 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 
相關問題