Getting rows that correspond to second occurrence of a value

3 ビュー (過去 30 日間)
the cyclist
the cyclist 2013 年 7 月 16 日
I have a matrix x:
x = [1 11;
1 12;
1 13;
2 26;
2 27;
2 28;
3 34;
4 41;
4 42]
x is guaranteed to be sorted by the first column and then the second column.
From the second column, I want the second occurrence (if any) for each unique value in the first column. So, in my example, I want my output to be
y = [12; 27; 42];
My first thought was to do this with accumarray:
y = accumarray(x(:,1),x(:,2),[],@(x)x(2));
but this errors because there is no second row when x(:,1)==3.
Thoughts? I don't need this to be fast, or handle huge arrays; I'd like it to be elegant!

回答 (4 件)

Jos (10584)
Jos (10584) 2013 年 7 月 16 日
x(strfind([1 diff(x(:,1)).']==0,[0 1])+1,2)
  2 件のコメント
Jos (10584)
Jos (10584) 2013 年 7 月 16 日
編集済み: Jos (10584) 2013 年 7 月 16 日
oh, and add a transpose at the end ;-)
Jan
Jan 2013 年 7 月 16 日
編集済み: Jan 2013 年 7 月 16 日
If you speak Matlab fluently, this looks elegant. For a beginner, this looks crude. +1

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


Jan
Jan 2013 年 7 月 16 日
[dummy, n, index] = RunLength(x(:, 1));
y = x(index(n >= 2) + 1, 2);

Cedric
Cedric 2013 年 7 月 16 日
編集済み: Cedric 2013 年 7 月 16 日
Here is another one liner:
>> regexp('','(?@ buffer=urlread(''http://en.wikipedia.org/wiki/Sea_urchin''); eval(''x(strfind([1 diff(x(:,1)).'''']==0,[0 1])+1,str2num(buffer(regexp(buffer, ''''\(?<=size from 6 to 1\)\\d'''', ''''start''''))))''))') ;
Isn't that elegant? ;-)
C.
  1 件のコメント
Jan
Jan 2013 年 7 月 31 日
If you speak Matlab fluently, this looks crude. For a beginner, this looks crude. +1

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


Andrei Bobrov
Andrei Bobrov 2013 年 7 月 31 日
編集済み: Andrei Bobrov 2013 年 8 月 1 日
[a,b] = unique(x(:,1),'first');
out = x(b(histc(x(:,1),a)>=2)+1,2);
or
[~,i1] = unique(x(:,1),'first');
[~,i2] = unique(x(:,1),'last');
out= x(i1((i2 - i1) > 0) + 1,2);

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by