How to rank a vector with repeats without MATLAB unique?

3 ビュー (過去 30 日間)
pedrogon
pedrogon 2016 年 6 月 27 日
編集済み: pedrogon 2016 年 6 月 28 日
Hi everybody,
I need to rank a vector and currently i'm using this two commands:
data_s=sort(data); [~,rank]=ismember(data,data_s);
For a vector data=[1 1 1 2 2 2 3 3 3] i get i=[1 1 1 4 4 4 7 7 7], but instead i would like to get i=[1 1 1 2 2 2 3 3 3], without using MATLAB function 'unique'. Anybody has any suggestion?
Thanks in advance.
  3 件のコメント
Guillaume
Guillaume 2016 年 6 月 27 日
"without using MATLAB function unique" Why?
It's possible to get the same output as unique using any of the set functions ismember, setdiff, union, etc., because internally they all call unique. But in that case, why not call unique directly?
If the reason is because it's an assignment, then using any of these functions just to avoid unique is probably not going to earn you a good mark. I would think that you'd be expected to come up with your own unique algorithm.
pedrogon
pedrogon 2016 年 6 月 28 日
編集済み: pedrogon 2016 年 6 月 28 日
First of all thank you for your attention,
I cannot use function unique, because i'm trying to implement a piece of code in a DSP using MATLAB 2010b and i don't know why it keeps giving me an error saying "floating constant is out of range". So that's why i was trying to implement an alternative to unique.

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

回答 (2 件)

Jan Orwat
Jan Orwat 2016 年 6 月 27 日
Do you want to replicate following behaviour?
[~, ~, rank] = unique(data);
rank = rank.';
There are thousands of ways to do that. For example:
[foo, bar] = sort(data);
rank(bar) = cumsum([1, diff(foo)~=0]);
But, as the cyclist wrote, are you sure?
  1 件のコメント
pedrogon
pedrogon 2016 年 6 月 28 日
Thank you for your answer, it is also a good alternative. In the algorithm that i'm trying to implement in a DSP, unique function should be used, so for the reasons that i listed above i need to replicate the function behaviour.

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


Jan
Jan 2016 年 6 月 27 日
data_s = sort(data);
result = cumsum([1, diff(data_s) ~= 0]);
  1 件のコメント
pedrogon
pedrogon 2016 年 6 月 28 日
編集済み: pedrogon 2016 年 6 月 28 日
That you for your answer, but your suggestion does not seem to work well. If i use:
data=[1 1 1 3 3 3 2 2 2];
I will get the following:
result=[1 1 1 2 2 2 3 3 3]

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

カテゴリ

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