Outputting even numbers using logical indexing and rem.
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
heehaw
el 11 de Dic. de 2015
Comentada: Image Analyst
el 11 de Dic. de 2015
I am having troubles figuring out How can I output only the even numbers in an array using logical indexing and the rem function?
Thanks!!
0 comentarios
Respuesta aceptada
Image Analyst
el 11 de Dic. de 2015
Try this:
% Create sample data.
m = randi(9, 1, 20)
% Get the logical indexes of even numbers ONLY.
evenLogicalIndexes = rem(m,2) == 0;
% Extract only those even elements from m
mEven = m(evenLogicalIndexes)
2 comentarios
Image Analyst
el 11 de Dic. de 2015
You could also use ~ to invert it:
oddLogicalIndexes = ~rem(m,2);
Same thing. Perhaps this way is slightly faster for very large arrays.
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!