How to find the K max or min values contained in a vector?

3 次查看(过去 30 天)
Before I attempt to write the code myself, I was wondering if a function already exists to find the k max values of a vector.
E.g. test_vec = [ 5 9 8 3 2 1 7 6]; k = 3; [max_vals, max_ids] = max_elems(test_vec, k); max_vals = [ 9 8 7] max_ids = [2 3 7]

回答(1 个)

Sean de Wolski
Sean de Wolski 2012-2-13
sort it 'descend' and extract the first k values.
  2 个评论
Christopher
Christopher 2012-2-13
I need the corresponding indices from the original vector per my example.
Walter Roberson
Walter Roberson 2012-2-13
[max_vals, max_ids] = sort(test_vec, 'descend');
max_vals = max_vals(1:k);
max_ids = max_ids(1:k);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by