Creating a matrix (B)of the same size as matrix (A), containing only even/odd values

1 view (last 30 days)
First time poster here, with something that should be simple for most...
I am needing to:
1. Create a 6x6 magic matrix (A)
2. Create a matrix of the same size, which displays all the even numbers, and displays the rest as zero.
3. Create a matrix of the same size, which displays all the odd numbers, and displays the rest as zero.
I've figured out how to extract the even/odd numbers and put them into a vector, but not a new matrix.
For this I am using:
Evens: A1=A(~mod(A,2))
Odds: A2=A(mod(A,2))
I am guessing there is a similar technique for creating a matrix, but the concept is currently lost on me.
Any help would be greatly appreciated!

Answers (1)

Image Analyst
Image Analyst on 20 Oct 2015
Close, but try it with multiplying by a mask:
A = magic(6)
evens = ~mod(A,2)
odds = mod(A,2)
Aevens = A .* evens
Aodds = A .* odds

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!