フィルターのクリア

command unique without sorting??

215 ビュー (過去 30 日間)
Nicoletta
Nicoletta 2014 年 3 月 25 日
コメント済み: Tom Toulouse 2021 年 5 月 26 日
i need to delete the double numbers in a vector and create a new vector without the doubles and maintaining the same order... i used unique but i dont know how to let them stay in the same order! for exemple for
v=[7 6 3 3 2 3 5 2 2 4 1 4]
it should be
v=[7 6 3 2 5 4 1]

回答 (4 件)

Honglei Chen
Honglei Chen 2014 年 3 月 26 日
You changed your question from yesterday, please don't do so. As to your new question, my answer above already covered it.
unique(v,'stable')

Andrei Bobrov
Andrei Bobrov 2014 年 3 月 26 日
編集済み: Andrei Bobrov 2014 年 3 月 26 日
for older releases of Matlab
[~,b] = unique(v,'first')
a_out = v(sort(b))
or
[a0,b0,c0] = unique(v,'first')
[~,ii] = sort(b)
[~,jj] = sort(ii);
a_out = a0(ii);
b_out = b0(ii);
c_out = jj(c);

Honglei Chen
Honglei Chen 2014 年 3 月 25 日
[vu,~,idx] = unique(v.','stable')
vs = accumarray(idx,ones(numel(v),1))
[vu vs]

Gabor
Gabor 2021 年 5 月 1 日
編集済み: Gabor 2021 年 5 月 1 日
In my opinion unique should be "stable" by default. If someone wants to sort, than there is sort commant that for. What does sorting by default in unique has to do with removing duplicate values? In same logic why doesnt sorting has default unique function built in? Or do I missing any point? For me it does not make any sense nor being logical.
  1 件のコメント
Tom Toulouse
Tom Toulouse 2021 年 5 月 26 日
It is because the 'stable' option did not exist on the first release of the unique function. Furthermore the simpliest way to get unique value is to sort the vector. So the stable option should be a little bit longer to execute.

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

カテゴリ

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