finding duplicate number and adjacent row value

1 回表示 (過去 30 日間)
Sobhan
Sobhan 2012 年 9 月 16 日
Suppose I have a matrix of values
v = [1 4; 1 5; 2 4; 2 4; 2 5; 2 5; 3 4; 3 7]
in column one the numbers are consecutive but with repetition, how can I find only the first numbers and corresponidng number in the other column, put them in a new matrix (v')? The new matrix should be like this from my example:
v'= [1 4; 2 4; 3 4]
I hope I am clear enough
Cheers
Sobhan
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 16 日
編集済み: Azzi Abdelmalek 2012 年 9 月 16 日
what if v= [1 4; 1 5; 2 4; 2 4; 2 5; 2 5; 3 4; 3 7;1 5;1 6]?

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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 16 日
v = [1 4; 1 5; 2 4; 2 4; 2 5; 2 5; 3 4; 3 7]
idx=diff(v(:,1))
v1=[];
if idx(1)==0
v1=[v1;v(1,:)]
end
for k=2:length(idx)
if idx(k)==0 & idx(k-1)~=0
v1=[v1; v(k,:)]
end
end
  1 件のコメント
Sobhan
Sobhan 2012 年 9 月 16 日
Dear Azzi, your solution worked perfectly! Thanks a lot. Wayne`s method gave an error. Anyhow, thanks a lot to both of you

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

その他の回答 (1 件)

Wayne King
Wayne King 2012 年 9 月 16 日
編集済み: Wayne King 2012 年 9 月 16 日
[~,iv] = unique(v(:,1),'stable');
vnew = v(iv,:);

カテゴリ

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