How to extract odd values and even values from a 500x500 black image ?
6 次查看(过去 30 天)
显示 更早的评论
I want to extract the even and odd numbers from 500x500 black matrix to turn them white without using loops or conditions statement.
3 个评论
回答(2 个)
Image Analyst
2022-3-6
编辑:Image Analyst
2022-3-6
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 个评论
DGM
2022-3-6
编辑:DGM
2022-3-6
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!