how remove tow of three consecutive same values in a vector

1 回表示 (過去 30 日間)
DuckDuck
DuckDuck 2014 年 4 月 23 日
コメント済み: the cyclist 2014 年 4 月 24 日
I have two vectors like this
a=[1,2,3,1,1,1,2,3,2,3,1,1,1,1,2,3,3,3,2,2,1],
where * b * is id of * a *
b=[1,2,3,11,12,13,20,21,25,27,31,32,33,34,36,40,41,42,47,48,50]
what i want to achieve is to keep the midle element of three consecutive elements or the even element if te consecuence is more than 3 elements. i want to keep the b[4] or b[11] and b[13] also the b[16]. how to achieve this?? Can any body help?
  9 件のコメント
DuckDuck
DuckDuck 2014 年 4 月 23 日
編集済み: DuckDuck 2014 年 4 月 23 日
@Geoff Hayes well i didn't thought about the odd case with more elements, but i thin i would keep the midle of the first three and the 5th, or i will go for every second, so in a bunch of five i would get 2. Thanks for the hint, though. But how about the case when i have 3 consecutive 1s in a, but they are not consecutive in b?
Geoff Hayes
Geoff Hayes 2014 年 4 月 23 日
I don't understand "..but they are not consecutive in b". I thought that it was only the consecutive integers in a that we were concerned with?

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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 23 日
a = [1,2,3,1,1,1,2,3,2,3,1,1,1,1,2,3,3,3,2,2,1];
b = [1,2,3,11,12,13,20,21,25,27,31,32,33,34,36,40,41,42,47,48,50];
c=diff(a)==0;
ii1=strfind([0 c 0],[0 1 1]);
ii2=strfind([0 c 0],[1 1 0])+1;
out=cell2mat(arrayfun(@(x,y) b(x+1:2:y),ii1,ii2,'un',0))
  2 件のコメント
DuckDuck
DuckDuck 2014 年 4 月 24 日
編集済み: DuckDuck 2014 年 4 月 24 日
ok but where are the first and last 1?
i need also the lonely 1s and those in pair.
the cyclist
the cyclist 2014 年 4 月 24 日
You did not mention anything about this in your original question. How were we to know this is what you needed?

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

その他の回答 (1 件)

the cyclist
the cyclist 2014 年 4 月 23 日
This code is just terrible, but I think it does what you want.
a = [1,2,3,1,1,1,2,3,2,3,1,1,1,1,2,3,3,3,2,2,1];
b = [1,2,3,11,12,13,20,21,25,27,31,32,33,34,36,40,41,42,47,48,50];
d = [];
currentRunLength = 1;
for i = 2:numel(a)
if a(i)==a(i-1)
currentRunLength = currentRunLength+1;
if ((currentRunLength==2) && (i~=numel(a)) && a(i)==a(i+1)) || ((currentRunLength>=4) && mod(currentRunLength,2)==0)
d = [d,b(i)];
end
else
currentRunLength = 1;
end
end
  3 件のコメント
the cyclist
the cyclist 2014 年 4 月 23 日
I think my code covers all cases. Do you have a counter-example?
Geoff Hayes
Geoff Hayes 2014 年 4 月 23 日
My mistake - I had read your a(i)==a(i-1) as ~= and so figured that a final check would be needed.
That being said, is it only the ids of the duplicates that are to be kept (from b) or should it be the ids of the single elements as well?

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by