MATLAB: How to extract even rows and columns from any Matrix of size n

even numbersmatrices

How do I extract a smaller Matrix or elements, containing only the even rows and columns from any Matrix of size n? Both the row and column must be even for it to return that. For example, if the Matrix is [3 2 1; 1 4 6, 2 2 4, 1 9 7], it should only return [2 2 4] as an answer. Thanks.

Best Answer

>> M = [3 2 1; 1 4 6; 2 2 4; 1 9 7];
>> M(all(~rem(M,2),2),:)
ans =
2 2 4