maxk from subset of array

조회 수: 11 (최근 30일)
Orr Streicher
Orr Streicher 2021년 8월 17일
댓글: Yazan 2021년 8월 17일
Hi,
I would like to solve the following prblem:
Lets say i have the vector :v=[10,22,3,-5,8,6,100,30,12,9]
I want to find the indicies and the values of the k largest number from a given subset, e.g: if the indices of the subset are [1,3,5,8] and k=2 the function will return: values=[10,30], indices=[1,8].
There is any suggestion how to implemet it?
Thanks

채택된 답변

Yazan
Yazan 2021년 8월 17일
clc, clear
v = [10,22,3,-5,8,6,100,30,12,9];
subIdx = [1,3,5,8];
k = 2;
% sort values and get corresponding indices
[val, idx] = sort(v(subIdx), 'descend');
% get only k values
val = val(1:k);
% get actual indices
Idx = subIdx(idx(1:k));
val
val = 1×2
30 10
Idx
Idx = 1×2
8 1
  댓글 수: 2
Orr Streicher
Orr Streicher 2021년 8월 17일
thanks!
Yazan
Yazan 2021년 8월 17일
You may also use Matlab native function maxk instead of sorting the values. This might save you some processing power.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Adding custom doc에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by