How to get the starting and ending index of repeated numbers in an array?

15 ビュー (過去 30 日間)
yashvin
yashvin 2015 年 6 月 22 日
コメント済み: yashvin 2015 年 6 月 26 日
Hi MY array is =[2 2 1 2 3 2 2 2 2 2 3 5 6 7 7 7 7 6 5]
I am trying to find the starting and index index of repeated numbers in my array
for example,
for number 2, i expect startingindex=1 and endingindex=2 as it is repeated and also startingindex=6th and endingindex=10.
for number 7, i expect startingindex=14 and endingindex=17
I have been trying using find so far but in vain

採用された回答

Jan
Jan 2015 年 6 月 22 日
An another question from the forum concerning a run-length-encoding. With FEX: RunLength:
X = [2 2 1 2 3 2 2 2 2 2 3 5 6 7 7 7 7 6 5];
[B, N, Ind] = RunLength(X);
Ind = [Ind, length(X)+1];
Multiple = find(N > 1);
Start = Ind(Multiple);
Stop = Ind(Multiple + 1) - 1;
  4 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 22 日
As indicated in the above answer, you can get RunLength function in this link http://www.mathworks.com/matlabcentral/fileexchange/41813-runlength
yashvin
yashvin 2015 年 6 月 23 日
Thanks!! works perfectly

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

その他の回答 (3 件)

Ingrid
Ingrid 2015 年 6 月 22 日
try using the diff function before calling find
  4 件のコメント
Stephen23
Stephen23 2015 年 6 月 22 日
編集済み: Stephen23 2015 年 6 月 22 日
@yashvin: notice that Ingrid mentioned two functions: diff and find. You only tried the first function, but you missed the find.
>> V = [2 2 1 2 3 2 2 2 2 2 3 5 6 7 7 7 7 6 5];
>> X = diff(V)~=0;
>> B = find([true,X]) % begin of each group
B =
1 3 4 5 6 11 12 13 14 18 19
>> E = find([X,true]) % end of each group
E =
2 3 4 5 10 11 12 13 17 18 19
>> D = 1+E-B % the length of each group
D =
2 1 1 1 5 1 1 1 4 1 1
Use logical indexing if you only want the groups with more than one element:
>> Y = D>1;
>> B(Y)
ans =
1 6 14
>> E(Y)
ans =
2 10 17
yashvin
yashvin 2015 年 6 月 26 日
Let me check it and get back to you. Thanks for your input

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


Andrei Bobrov
Andrei Bobrov 2015 年 6 月 22 日
編集済み: Andrei Bobrov 2015 年 6 月 22 日
A=[2 2 1 2 3 2 2 2 2 2 3 5 6 7 7 7 7 6 5];
ii = [0, diff(A(:)')==0,0];
i1 = strfind(ii,[0 1]);
i2 = strfind(ii,[1 0]);
out = [A(i1)',i1(:),i2(:)];
  2 件のコメント
yashvin
yashvin 2015 年 6 月 26 日
I tried your solution. It is working perfectly so far.
yashvin
yashvin 2015 年 6 月 26 日
@ Andrei Bobrov
But i am new to strfind. According to
It looks for pattern and the example in the page treats pattern in string mostly. Can you please a little description of the code you had written. Thanks!

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


Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 22 日
編集済み: Azzi Abdelmalek 2015 年 6 月 22 日
A=[2 2 1 2 3 2 2 2 2 2 3 5 6 7 7 7 7 6 5];
d1=~diff(A).*(1:numel(A)-1);
idx1=d1.*[1 ~d1(1:end-1)];
idx2=circshift(d1.*[~d1(2:end) 1],[0 1]);
ii1=~~idx1;
out=[A(ii1);idx1(ii1) ;idx2(~~idx2)+1];

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by