MATLAB: I have the following code (recognizing individuals pixels of a matrix). For a high number of images, this is very time consuming. Therefore, ¿Could the loop be avoided ? Thanks!

digital image processingMATLAB

V=zeros(x,y,num_images,'uint16')
for k=1:num_images
for i= 1:x
for j= 1:y
if B(i,j,k)==0
V(i,j,k)= A(i,j,k)/3
else
V(i,j,k)= A(i,j,k)/3+(2^15)
end
end
end
end

Best Answer

It's difficult to know the relationship between A, B and V from your example code but I am assuming they are of equal dimensions. In which case I think you probably want something like this instead of the loop.
V = zeros(size(B));
V(B == 0) = A(B == 0)/3;
V(B ~= 0) = A(B ~=0)/3 + 2^15;