本文介绍了我如何在MATLAB中编码某些元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从0到3做了两个随机数.
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);
现在我想拆分M的每3个元素,并用另一个元素对其进行编码.例如000按000000、110按000001、001按00001、100按0001、101按001、010按01、011按1.
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))
推荐答案
M
是可以使用Lookup_Out
以此方式映射的字符串:
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
这篇关于我如何在MATLAB中编码某些元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!