How to extract odd values and even values from a 500x500 black image ?
3 views (last 30 days)
Show older comments
I want to extract the even and odd numbers from 500x500 black matrix to turn them white without using loops or conditions statement.
3 Comments
Image Analyst
on 6 Mar 2022
You're still not clear. Zero is neither an odd number or an even number.
Answers (2)
Image Analyst
on 6 Mar 2022
Edited: Image Analyst
on 6 Mar 2022
Do you have any other numbers other than 0? If so you can use rem(M, 2). Observe:
M = magic(5)
mCopy = M;
oddIndexes = rem(mCopy, 2) == 1
oddNumbers = M(oddIndexes) % Extract the odd numbers.
% Turn the odd numbers to white (255)
mCopy(oddIndexes) = 255
evenIndexes = rem(mCopy, 2) == 0
evenNumbers = M(evenIndexes)
% Turn the even numbers to white (255)
mCopy = M; % Reset mCopy so there are no 255's in there anymore.
mCopy(evenIndexes) = 255
0 Comments
DGM
on 6 Mar 2022
Edited: DGM
on 6 Mar 2022
Consider the matrix:
A = randi([10 99],5,5)
% get elements from odd linear indices
oddelems = A(1:2:numel(A))
% get elements from even linear indices
evenelems = A(2:2:numel(A))
That's linear indices. Maybe you want to address odd or even rows/columns:
% get elements from odd rows
oddrows = A(1:2:size(A,1),:)
% get elements from even rows
evenrows = A(2:2:size(A,1),:)
... and so on for columns
EDIT: I'm dumb. I forgot you wanted to set pixels to white.
The above still holds. You're still trying to address your array. Just apply that to an assignment:
B = zeros(5)
B(1:2:numel(B)) = 1 % set odd linear indices to 1
... and so on
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!