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!
1 次查看(过去 30 天)
显示 更早的评论
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
1 个评论
KSSV
2016-12-22
First you terminate the outputs with ;. Else it will take hell lot of time, because it will print result on the screen.
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
采纳的回答
David Barry
2016-12-22
编辑:David Barry
2016-12-22
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;
0 个评论
更多回答(2 个)
John BG
2016-12-22
there is no need to initialise the void V, go straight to the indices returned from functions
- find( zeros())
- find( nonzeros())
A=A/3
V(find(zeros(B)))=A(find(zeros(B)))+2^15
V(find(nonzeros(B)))=A(find(nonzeros(B)))
if you find my answer useful would you please mark it as Accepted Answer by clicking on the ACCEPT ANSWER button?
thanks in advance for time and attention
John BG
1 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!