Extract sequences from vector

8 ビュー (過去 30 日間)
Peter
Peter 2013 年 2 月 27 日
回答済み: Saeid Abrari 2020 年 10 月 6 日
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 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 27 日
編集済み: Azzi Abdelmalek 2013 年 2 月 27 日
Are you looking for all the sequences?, for example (2,5,7,3)
Peter
Peter 2013 年 2 月 28 日
Hello Azzi!
I do not really understand what you mean with (2,5,7,3). Maybe my example was a bit confusing! I search for all values in U that could be used to build s, but only in chronological order, so I realized I forgot some in my example. U(1,1,12) would also correspond to s, or U(2,9,12), U(1,2,5) etc.
At the end of the day, I want to find the values (2,2,3) in U that are closest together, so in my example I would search for U(10,11,12). But: it can happen in my real data that the values of s are only in U with interruptions, otherwise it would be a simple task. My plan was to to first find all possible triples in U that could be used to build s and then find out via their indexes which triple is closest together.
Is this clearer now?
Kind regards and many thanks, Peter

サインインしてコメントする。

回答 (4 件)

Babak
Babak 2013 年 2 月 27 日
perms
nchoosek
might be helpful in finding combinations of the elements of different size vectors.

Peter
Peter 2013 年 2 月 28 日
編集済み: Peter 2013 年 2 月 28 日
I think I could come up with a solution myself
x=[2,2,5,7,3,4,4,4,2,2,2,3]'; y=[2,2,3]';
p=0; s=0; t=0;
for i=1:length(x);
for t=1+s:length(x); % here the code goes down one line
if p<length(y);
if x(t,1)==y(p+1,1);
codes(p+1,s+1)=t;
p=p+1;
end;
end;
end;
s=s+1;
p=0;
end;
codes=codes';
codes=unique(codes,'rows');
k=find(sum(codes==0,2)>0);
codes(k,:)=[]; % incomplete triples from end of the code are removed
codes=codes';
The code goes through all records in U (here called x) and looks up s (here called y). The final matrix in codes contains all indices in x from which y can be built from.
  1 件のコメント
Babak
Babak 2013 年 2 月 28 日
Nice solution. I rather let "codes" be a cell object with option of having empty cells. Then you can support finding zero elements in y and your
k=find(sum(codes==0,2)>0);
line wouldn't fail for length(y)>3.

サインインしてコメントする。


Saeid Abrari
Saeid Abrari 2020 年 10 月 6 日
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;

Saeid Abrari
Saeid Abrari 2020 年 10 月 6 日
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

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by