Converting matrix entries satisfying a threshold condition.
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Paul Fishback
el 29 de En. de 2015
Editada: Zakaria Abdelli
el 21 de Oct. de 2022
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?
Respuesta aceptada
Star Strider
el 29 de En. de 2015
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 comentarios
Star Strider
el 7 de Mzo. de 2020
@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
el 21 de Oct. de 2022
Editada: Zakaria Abdelli
el 21 de Oct. de 2022
how do i do it with 3 and others 0 can you give me a hint pleas im just a newbee???
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!