Binarize a metrix with some threshold
40 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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
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 个评论
Image Analyst
2020-3-16
Or you can use the other input of sort() to avoid flipping:
[sortedI1, sortOrder] = sort(I1, 'ascend'); % No need to flip for ascending order
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!