Extract sequences from vector
    9 views (last 30 days)
  
       Show older comments
    
Dear Forum members,
I have a question I could not find any solution for. I have a vector, let's say
U=[2,2,5,7,3,4,4,4,2,2,2,3].
I also have a sequence, which is
s=[2,2,3].
I now want to find all the indexes in U, where my sequence s could be built from. In my case, that would be
(1,2,5), (9,10,12), (10,11,12), (9,11,12). So the problem is the sequences have to be build first from U and do not exist as in s. I find this quite tough and would be happy if someone had a solution.
I have Matlab 7.13.
Thanks a lot! Peter
2 Comments
  Azzi Abdelmalek
      
      
 on 27 Feb 2013
				
      Edited: Azzi Abdelmalek
      
      
 on 27 Feb 2013
  
			Are you looking for all the sequences?, for example (2,5,7,3)
Answers (4)
  Babak
      
 on 27 Feb 2013
         perms
 nchoosek
might be helpful in finding combinations of the elements of different size vectors.
0 Comments
  Saeid Abrari
 on 6 Oct 2020
        for dig=1:16
        ndata = length(Frame);
        ndata_1=length(digit(dig,:));
        n_diff=abs(ndata-ndata_1);
        Sound_1=[digit(dig,:)';zeros(n_diff,1)];
        sx = sum(Frame.^2); 
        sy = sum(Sound_1.^2);
        maxlag = ndata-1;
        cnt = 0;
        for lag = -maxlag:maxlag  
            cnt = cnt + 1;
            sxy = 0;
            for i=1:ndata
                j = i + lag;
                if j>0 && j<=ndata
                    sxy = sxy + Frame(i) * SoundVec_1(j);
                end
            end
            cc(cnt) = sxy / sqrt(sx*sy);  % correlation coefficient 
            ck(cnt) = sxy;                % cross-correlation value 
            lags(cnt) = lag;
        end
        simil(dig)=max(cc);
        [~,i] = max(cc);
        Ts=1/Fs;
        t1=Ts:Ts:FrameLen*Ts;
        norm_ck=cc/max(cc);
        lx = (length(norm_ck));
        half = ceil(lx/2); 
        norm_ck_p=norm_ck(1:half);
        td = i - ndata;
        tau1=lags/Fs;
0 Comments
  Saeid Abrari
 on 6 Oct 2020
        for dig=1:12
        ndata = length(Frame);
        ndata_1=length(digit(dig,:));
        n_diff=abs(ndata-ndata_1);
        Sound_1=[digit(dig,:)';zeros(n_diff,1)];
        sx = sum(Frame.^2); 
        sy = sum(Sound_1.^2);
        maxlag = ndata-1;
        cnt = 0;
        for lag = -maxlag:maxlag  
            cnt = cnt + 1;
            sxy = 0;
            for i=1:ndata
                j = i + lag;
                if j>0 && j<=ndata
                    sxy = sxy + Frame(i) * SoundVec_1(j);
                end
            end
            cc(cnt) = sxy / sqrt(sx*sy);  % correlation coefficient 
            ck(cnt) = sxy;                % cross-correlation value 
            lags(cnt) = lag;
        end
        simil(dig)=max(cc);
        [~,i] = max(cc);
        Ts=1/Fs;
        t1=Ts:Ts:FrameLen*Ts;
        norm_ck=cc/max(cc);
        lx = (length(norm_ck));
        half = ceil(lx/2); 
        norm_ck_p=norm_ck(1:half);
        td = i - ndata;
        tau1=lags/Fs;
  %Find the maximum corellation index(digit)
    if(max(simil)>0.5)
        [seqm sequ(s_loc)]=max(simil);
        s_loc=s_loc+1;
    end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!