Converting matrix entries satisfying a threshold condition.

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?

댓글 수: 1

Never mind... Forgot I can apply the floor function to a matrix.

댓글을 달려면 로그인하십시오.

 채택된 답변

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

Or, instead of the last 3 lines:
M = M > someThreshold;
What if my threshold is not a specific number but a percentile? for example, set the top 20% values to 1 and the rest to 0.
@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개)

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

질문:

2015년 1월 29일

편집:

2022년 10월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by