Outputting even numbers using logical indexing and rem.
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2015-12-11
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 个评论
Image Analyst
2015-12-11
You could also use ~ to invert it:
oddLogicalIndexes = ~rem(m,2);
Same thing. Perhaps this way is slightly faster for very large arrays.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!