Converting matrix entries satisfying a threshold condition.

17 次查看(过去 30 天)
I have a matrix of numeric entries and wish to convert each entry above a specified threshold to one and all other entries to zero. This has to be an elegant way to do this without a "for" loop. Perhaps using the "find" command in some manner?

采纳的回答

Star Strider
Star Strider 2015-1-29
There are likely a number of ways.
One approach:
M = magic(6);
Mm = mean(M(:));
M(M>Mm) = 1;
M(M~=1) = 0;
It sets all the entries greater than the mean to 1, and then uses that result to set the rest to 0.
  4 个评论
Star Strider
Star Strider 2020-3-7
@Tamir Eisenstein —
M = randi(99, 10); % Create Matrix
Msort = sort(M(:),'descend'); % Sorted Vector
Threshold = Msort(fix(numel(M)*0.2)); % Find Top 20% Value
Out = zeros(size(M))~=0; % Create Logical Matrix
Out(M >= Threshold) = 1; % Desired Result
Zakaria Abdelli
Zakaria Abdelli 2022-10-21
编辑:Zakaria Abdelli 2022-10-21
how do i do it with 3 and others 0 can you give me a hint pleas im just a newbee???

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by