Calculate t maximums in single vector

I have an iterative program for linear block codes (k,n), and at some point, I add code-words base10
The addition result in a vector of size (1,n)
I need to find the maximum (t) elements in this vector without going into loops operations, as the time of program execution is very important.
Example: if my Vector is (V)=(1,n)
V=[68 61 35 37 18 80 50 36 78 24 85 82 85 38 39 87 47 58 70 97 55 64 58 93 87 17 18 25 76 20 99 71]
I want a direct command that can calculate the maximum 5 elements of the Vector (V).
Kindly help.

 採用された回答

Star Strider
Star Strider 2016 年 12 月 3 日

0 投票

You can always create your won function file from this code to produce your ‘direct command’:
V=[68 61 35 37 18 80 50 36 78 24 85 82 85 38 39 87 47 58 70 97 55 64 58 93 87 17 18 25 76 20 99 71];
Vs = sort(V,'descend');
Out = Vs(1:5)
Out =
99 97 93 87 87

4 件のコメント

Moe Joe
Moe Joe 2016 年 12 月 3 日
編集済み: Moe Joe 2016 年 12 月 3 日
But, I still need to return their locations (positions), that is, I want to return a vector of (1,n) with ONES in the position of those maximum elements.
bio lim
bio lim 2016 年 12 月 3 日
ismember(V,Out)
ans =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 0 0 1 0
Moe Joe
Moe Joe 2016 年 12 月 3 日
Perfect, thanks a lot.
Star Strider
Star Strider 2016 年 12 月 3 日
My apologies for the delay. The other option would be to ask for two outputs from the sort function. The second will give you the indices of the sorted elements in the original vector.
From the documentation:
  • [B,I] = sort(_) also returns a collection of index vectors for any of the previous syntaxes. I is the same size as A and describes the arrangement of the elements of A into B along the sorted dimension. For example, if A is a numeric vector, B = A(I).

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

その他の回答 (1 件)

John D'Errico
John D'Errico 2016 年 12 月 3 日

0 投票

Sort them. Then take the first 5 elements from the sort. WTP?
No. It is not ONE single command. Not an option. Of course, nothing stops you from writing a function that does exactly that, in which case it will be one line.

1 件のコメント

Moe Joe
Moe Joe 2016 年 12 月 3 日
編集済み: Stephen23 2016 年 12 月 3 日
Already did this function:
% This is a function locates the positions of maximum (t) numbers in a
function [v]=maxvic(X,t);
vector V(1,n).
n=length(X);
v=zeros(1,n);
for i = 1:t;
[y j]=max(X,[],2);
v(j)=1;
X(j)=0;
end
end
But thought the more expert than I could advice a better solution.

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

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

2016 年 12 月 3 日

編集済み:

2016 年 12 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by