How to extract odd values and even values from a 500x500 black image ?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I want to extract the even and odd numbers from 500x500 black matrix to turn them white without using loops or conditions statement.
3 comentarios
Image Analyst
el 6 de Mzo. de 2022
You're still not clear. Zero is neither an odd number or an even number.
Respuestas (2)
Image Analyst
el 6 de Mzo. de 2022
Editada: Image Analyst
el 6 de Mzo. de 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 comentarios
DGM
el 6 de Mzo. de 2022
Editada: DGM
el 6 de Mzo. de 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 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!