I have a matrix
8 23 44 19
44 5 62 1
7 6 12 33
6 55 24 8
and I want to find 3 largest values(reserve duplicate matches),and then make the other be 0
just like
0 0 44 0
44 0 62 0
0 0 0 0
0 55 0 0
I have read some book ,but I still have no idea
help me plz

3 件のコメント

Aditya Verma
Aditya Verma 2020 年 6 月 16 日
Hello, could you please specify what you have tried until now, and in which part you are facing problem.
chung yen chang
chung yen chang 2020 年 6 月 18 日
q=[6 7 3 2
5 3 6 4
7 7 5 3
7 9 10 5]
[x,y]=sort(q(:))
q(y(1:end-3))=0
in this case matrix can't preserve max three values
chung yen chang
chung yen chang 2020 年 6 月 18 日
the answer would be
q =
0 0 0 0
0 0 0 0
0 7 0 0
0 9 10 0

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

 採用された回答

madhan ravi
madhan ravi 2020 年 6 月 16 日
編集済み: madhan ravi 2020 年 6 月 16 日

2 投票

MaX = maxk(matrix(:), 3);
Wanted = ismember(matrix, MaX) .* matrix
% for older versions
m = sort(matrix(:),'descend');
Wanted = ismember(matrix, m(1:3)) .* matrix

11 件のコメント

Aditya Verma
Aditya Verma 2020 年 6 月 16 日
While this works for the example provided by OP. This would not work for all cases.
madhan ravi
madhan ravi 2020 年 6 月 16 日
Illustrate.
Aditya Verma
Aditya Verma 2020 年 6 月 16 日
Consider the following:
matrix = [8 23 46 19;
47 5 62 1 ;
7 6 12 33 ;
6 55 24 8];
[MaX, index] = maxk(matrix(:),4);
Wanted = zeros(size(matrix));
Wanted(index) = MaX
It gives the following output:
0 0 46 0
47 0 62 0
0 0 0 0
0 55 0 0
Which preserves four max values.
madhan ravi
madhan ravi 2020 年 6 月 16 日
編集済み: madhan ravi 2020 年 6 月 16 日
Isn’t that what the OP is looking for? Change 4 to 3.
Aditya Verma
Aditya Verma 2020 年 6 月 16 日
編集済み: Aditya Verma 2020 年 6 月 16 日
OP mentioned: "I want to find 3 largest values(reserve duplicate matches),and then make the other be 0". If I understand correctly, it means that OP wants to preserve max three values, but preserve the duplicates. So, if my elements in matrix in descinding order are: 9, 9, 9, 8, 8, 7, 7, 3, 2, 0, -1, -1. Then all the 9s, 8s and 7s should persist, and rest all should be 0.
In the example I provided above, it is preserving max 4 values.
madhan ravi
madhan ravi 2020 年 6 月 16 日
編集済み: madhan ravi 2020 年 6 月 16 日
Yes, you’re right. Rectified.
chung yen chang
chung yen chang 2020 年 6 月 18 日
編集済み: chung yen chang 2020 年 6 月 18 日
Yes AV, that is what I mean. Actually I have try the function "maxk" to use it before ,but this function can't run in 2015b so I try to seek another method
madhan ravi
madhan ravi 2020 年 6 月 18 日
編集済み: madhan ravi 2020 年 6 月 18 日
Chung did you even see my answer with the second option even though I specifically mentioned for “older versions”. Come on!
chung yen chang
chung yen chang 2020 年 6 月 18 日
Madhan I apologize for that ,I misread .Actually this a great method
madhan ravi
madhan ravi 2020 年 6 月 18 日
In the command window just call the function
Matrix = randi(10,4); % an example
Wanted = Mx(Matrix)
% function definition
function Wanted = Mx(matrix) % save it as a separate file called Mx.m
... the Code
end
Stephen23
Stephen23 2020 年 6 月 18 日
Note that this answer does not "find 3 largest values" as the question requested:
>> matrix = [4,4,4;4,4,4;3,2,1]
matrix =
4 4 4
4 4 4
3 2 1
>> m = sort(matrix(:),'descend');
>> Wanted = ismember(matrix, m(1:3)) .* matrix
Wanted =
4 4 4
4 4 4
0 0 0
The problem is caused by the use of sort. See my answer for the correct solution.

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

その他の回答 (1 件)

Stephen23
Stephen23 2020 年 6 月 16 日

0 投票

Where M is your matrix:
>> U = unique(M(:));
>> X = ismember(M,U(end-2:end));
>> M(~X) = 0
M =
0 0 44 0
44 0 62 0
0 0 0 0
0 55 0 0

3 件のコメント

chung yen chang
chung yen chang 2020 年 6 月 18 日
Thanks, bro this answer can work very well
Stephen23
Stephen23 2020 年 6 月 18 日
編集済み: Stephen23 2020 年 6 月 18 日
"Thanks, bro this answer can work very well"
Note my answer actually gives the output that you asked for (unlike the answer that you accepted):
>> M = [4,4,4;4,4,4;3,2,1]
M =
4 4 4
4 4 4
3 2 1
>> U = unique(M(:));
>> X = ismember(M,U(end-2:end));
>> M(~X) = 0
M =
4 4 4
4 4 4
3 2 0
madhan ravi
madhan ravi 2020 年 6 月 18 日
However maxk(...) gives the right answer xD, but i do agree the loophole.

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

カテゴリ

ヘルプ センター および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by