Search code examples
matlabfor-loopcomparesignalsspeech

how to compare values of an array with a single value in matlab


Can anyone tell me how to compare this for loop array value pp1 with the single value of pp. If the value of pp is present in pp1 then it must show 1 or must show 0. I'm getting 1 only the last value of pp1. The code is:

[pp,pf1]=pitchauto(x,fs);

for ix=1:2
    V='.wav';
    ie=num2str(ix);
    Stc=strcat(ie,V);
    [x1,fs1]=wavread(Stc);
    figure,plot(x1);
    title('Test Audio');
    [pp1,pf1]=pitchauto(x1,fs1);
end

if (pp==pp1)
   msgbox('Matching');
else
   msgbox('Not Matching');
end

Kindly do reply with the correct answers.


Solution

  • You calculate a value for pp1 each time, do nothing with it, then let the next loop iteration overwrite it. To make use of it, either put the test inside the loop:

    for ix=1:2
        V='.wav';
        ie=num2str(ix);
        Stc=strcat(ie,V);
        [x1,fs1]=wavread(Stc);
        figure,plot(x1);
        title('Test Audio');
        [pp1,pf1]=pitchauto(x1,fs1);
    
        if (pp==pp1)
            msgbox('Matching', num2str(ix)); % show the index number as msgbox title
        else
            msgbox('Not Matching', num2str(ix));
        end
    end
    

    or collect the values of pp1 in an array to test afterwards:

    for ix=1:2
        V='.wav';
        ie=num2str(ix);
        Stc=strcat(ie,V);
        [x1,fs1]=wavread(Stc);
        figure,plot(x1);
        title('Test Audio');
        [pp1(ix),pf1]=pitchauto(x1,fs1); % assuming pitchauto returns a scalar
    end
    
    matchidx = (pp == pp1);
    if any(matchidx)
        msgbox(strcat('Matching indices: ', num2str(find(matchidx))));
    else
        msgbox('Not Matching');
    end
    

    If the values aren't scalar, then this approach is a bit more difficult - you could still use a matrix to collect equal-sized vectors, or a cell array to collect anything - but it's probably easier to stick with the first approach in that case.