Outputting even numbers using logical indexing and rem.
조회 수: 12 (최근 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!