MATLAB Answers


Converting matrix entries satisfying a threshold condition.

Asked by Paul Fishback on 29 Jan 2015
Latest activity Edited by Image Analyst
on 29 Jan 2015
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 Comment

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

Sign in to comment.


1 Answer

Answer by Star Strider
on 29 Jan 2015
 Accepted Answer

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.

  1 Comment

Or, instead of the last 3 lines:
M = M > someThreshold;

Sign in to comment.