MATLAB Answers

Binarize a metrix with some threshold

27 ビュー (過去 30 日間)
Tamir Eisenstein
Tamir Eisenstein 2020 年 3 月 16 日
コメント済み: Arthur Roué 2020 年 3 月 16 日
Hi Matlab experts,
How can I binarize a matrix according to a certain threshold? for exapmle the highest 400 values or the highest 20% of the values?
Thanks!
Tamir

回答 (2 件)

Bhaskar R
Bhaskar R 2020 年 3 月 16 日
編集済み: Bhaskar R 2020 年 3 月 16 日
A = randi([0, 1000], 5) % dummy marix
A =
392 277 317 766 646
656 46 951 795 710
171 97 34 187 755
706 824 439 490 276
31 695 381 446 680
>> A>400 % command to compare with 400 threshold
ans =
5×5 logical array
0 0 0 1 1
1 0 1 1 1
0 0 0 0 1
1 1 1 1 0
0 1 0 1 1
>> A>(400*0.2)
ans =
5×5 logical array
1 1 1 1 1
1 0 1 1 1
1 1 0 1 1
1 1 1 1 1
0 1 1 1 1
  1 件のコメント
Tamir Eisenstein
Tamir Eisenstein 2020 年 3 月 16 日
Dear Bhaskar, thank you, but I think you misunderstood my two examples:
1) Let's say I have a matrix with 1000 components - I want the highest 400 values to be assigned a value of 1, and the rest to become 0.
2) Let's say I have a matrix with 1000 components - I want the highest 20% values to be assigned a value of 1, and the rest to become 0.

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


Arthur Roué
Arthur Roué 2020 年 3 月 16 日
You can use the 2nd output of sort function.
The code bellow should do what you want.
% Random matrix
M = randi([0, 1000], 5);
M =
699 480 806 28 500
198 905 577 490 471
30 610 183 168 59
744 618 240 979 682
500 860 887 713 42
% Sort
[~,I1] = sort(M(:));
[~,I2] = sort(flipud(I1)); % flip for ascending order
% Reshape
Index = (1:numel(M))';
Index = Index(I2);
Index = reshape(Index, size(M));
Index =
8 16 5 25 13
19 2 12 15 17
24 11 20 21 22
6 10 18 1 9
14 4 3 7 23
% For the 5 greatest values
Index <= 5
ans =
5×5 logical array
0 0 1 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 1 0
0 1 1 0 0
  2 件のコメント
Arthur Roué
Arthur Roué 2020 年 3 月 16 日
Indeed, you could to that, thanks for the complement.

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

Community Treasure Hunt

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

Start Hunting!

Translated by