【问题标题】:MATLAB morse codeMATLAB摩尔斯电码
【发布时间】:2017-10-28 21:15:06
【问题描述】:

编写函数 tokenizeSignal(signal),它接受上面的信号并计算顺序出现的 0 和 1 的数量。输出应该是一个二维数组,其中第 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), [])

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

【问题讨论】:

    标签: matlab function return morse-code


    【解决方案1】:

    它不起作用的原因是因为您依赖关键字 'ans',可以从工作区访问,而不是在函数内部,指的是 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), []) ;
    

    【讨论】:

    • 谢谢,这很有帮助。现在我收到一个错误,因为在某些测试条件下 matA 和 matB 的大小不同。我应该如何重塑它们以获得令牌?
    • 你可以返回一个单元格数组
    • 代币 = {matA(:) ; matB(:)};用这个替换函数的最后一行。 HTH, PSS
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    相关资源
    最近更新 更多