Search code examples
matlabsplitbinaryencode

How i can encode some elements in MATLAB?


I made two random numbers from 0 to 3.

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

then i add every two bits of them. then i convert it to binary.

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

because i wanted to count transitions, i write this code:

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);

now i want to split every 3 elements of M and encode them By another elements. for example 000 By 000000, 110 By 000001, 001 By 00001, 100 By 0001, 101 By 001, 010 By 01, 011 By 1.

I used this method but it doesn't work. What is wrong with it?

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

Solution

  • M is an string that can be mapped using Lookup_Out in this way:

    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