When L is not unique, how can I choose any of L

1 回表示 (過去 30 日間)
Muhammad Usman
Muhammad Usman 2018 年 11 月 22 日
コメント済み: Guillaume 2018 年 11 月 22 日
let say i have a vector
S=[ 7 1 15 13 2 14 6 10 12 11 4 8 3 9 5];
and after following certain procedure i got the indices of corresponding to the vector given i.e.,
L =
1 1 2 2 2 3 3 4 5 5 3 4 3 5 4
as it is clear from the indices that these are not unique, and I want to pick the maximum number by maximum index
I am getting wrong when i apply
z = max(L(:));
k = find(L==z,1,'last')
Thanks
  3 件のコメント
Rik
Rik 2018 年 11 月 22 日
If you want an index to the maximum value, you can also use the second output of max (you can use end on that vector to select the last one). That would be equivalent to the code you wrote, so it is unclear to me as well what you mean.
Muhammad Usman
Muhammad Usman 2018 年 11 月 22 日
max (L(:)) = 5;
for that index there are three numbers in S i.e., 12, 11 and 9.
Let say i want to pick number 11

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

採用された回答

madhan ravi
madhan ravi 2018 年 11 月 22 日
編集済み: madhan ravi 2018 年 11 月 22 日
[value,idx] = max(L)
s=S(L==value)
result=s(randi([1 numel(s)],1)) %picks any of L out of three numbers
  3 件のコメント
madhan ravi
madhan ravi 2018 年 11 月 22 日
編集済み: madhan ravi 2018 年 11 月 22 日
Not really because you only get one output which is 12 the max but there has to be 9,11 and 12 in which finally one amongst these three numbers have to be picked.
Guillaume
Guillaume 2018 年 11 月 22 日
Yes, max only returns the index of the first value equal to the maximum, not all of them.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by