马特拉布莫尔斯电码



编写函数tokenizeSignal(signal),它获取上面的信号并计算按顺序出现的0和1的数量。输出应是一个 2D 数组,其中列 1 是显示的数量,列 2 是它是什么标记(0 或 1)。我有以下代码可以工作,直到我把它放在一个函数中。例如

sig =[1 1 0 0 0 0 0 1 1 0 1 0 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 0];
tsig = abs(sig);  
dsig = diff([1 tsig 1]);
startIndex = find(dsig<0);
endIndex = find(dsig>0)-1;
duration = endIndex-startIndex+1;
stringIndex = (duration >= 2);
d=find(stringIndex==0);
matA=[duration;zeros(1,size(duration,2))];
matA=matA';
wsig = abs(sig);  
rsig = diff([0 wsig 0]);
startIndex = find(rsig < 0);
endIndex = find(rsig > 0)-1;
duration = endIndex-startIndex+1;
abs(duration);
stringIndex = (duration >= 2);
d=find(stringIndex==0);
type=[1];
matB=[ans;ones(1,size(ans,2))];
matB=matB';
token=reshape([matA(:) matB(:)]',size(matA,1)+size(matB,1), [])

这返回了我们需要的内容,但是当我们将上面的代码放入函数标头并在结论中键入 end 时,它不再返回任何内容。这是为什么呢?

它不起作用的原因是因为您依赖关键字"one_answers",可从工作区访问,而不是在函数内部访问,并引用 abs(duration)

这会在函数中复制脚本:

function   tokens = tokenizeSignal( sig )
tsig = abs(sig);  
dsig = diff([1 tsig 1]);
startIndex = find(dsig<0);
endIndex = find(dsig>0)-1;
duration = endIndex-startIndex+1;
matA=[duration;zeros(1,size(duration,2))];
matA=matA';
wsig = abs(sig);  
rsig = diff([0 wsig 0]);
startIndex = find(rsig < 0);
endIndex = find(rsig > 0)-1;
duration = endIndex-startIndex+1;
yourAns = abs(duration);
matB=[yourAns;ones(1,size(yourAns,2))];
matB=matB';
tokens=reshape([matA(:) matB(:)]',size(matA,1)+size(matB,1), []) ;

最新更新