How i can encode some elements in MATLAB?

二次信任 提交于 2020-01-05 06:27:49

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/47235922/how-i-can-encode-some-elements-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!