How can I leave only the 10 largest elements of a matrix?

14 ビュー (過去 30 日間)
Jae Min Lee
Jae Min Lee 2018 年 9 月 19 日
コメント済み: Walter Roberson 2018 年 9 月 19 日
I would like to make the remainder of the magnitude matrix zero except for the 10 largest values.
The code is shown below.
while hasFrame(vidReader)
Fr2 = rgb2gray(readFrame(vidReader)); % get second frame
Fr2 = im2double(Fr2);
% compute Vx, Vy by HS
[Vx, Vy] = OpticalFlow(Fr1, Fr2, alpha, N);
m = getMagnitude(Vx, Vy);
Fr1 = Fr2; % Fr2 becomes first frame of next sequence
[rows, cols] = size(m);
end
magnitude matrix = m

採用された回答

Walter Roberson
Walter Roberson 2018 年 9 月 19 日
編集済み: Walter Roberson 2018 年 9 月 19 日
[vals, index] = sort(m(:), 'descend');
selected_index = index(1:10);
mt = 0 * m; %same size and type but all 0
mt(selected_index) = m(selected_index);
m = mt;
Note: you need to figure out how you want to handle ties.
  3 件のコメント
Steven Lord
Steven Lord 2018 年 9 月 19 日
mt = 0 * m; %same size and type but all 0
This is true unless m contains a nonfinite value.
I'd prefer calling maxk with two output arguments to generate vals and selected_index, filling all of m with 0, then using those maxk outputs to put the maximum values back in place.
Walter Roberson
Walter Roberson 2018 年 9 月 19 日
Good point.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by