parfor loop??? making it faster how?

3 次查看(过去 30 天)
Mandar
Mandar 2014-2-28
编辑: Matt J 2014-2-28
i have wrote something like this to search for a color reference in live feed from webcam:
opengl hardware
while (nFrame<z)
b = step(vidobj);
n=0;
yb=size(b,1); %rows
zb=size(b,2); %columns
%u=zeros(zb,2); %------------------------------
v=0;
for g=1:1:yb
for h=1:1:zb
if (b(g,h,1)>=rot_min) && (b(g,h,1)<=rot_max)
n=n+1;
v(n,1)=g;
v(n,2)=h;
end
end
end
v;
%end of rot search
so now i want to exploit the core i5 to use all for cores and make the code even more faster...

回答(2 个)

Mandar
Mandar 2014-2-28
i know the syntax. here are the probs that i am facing:
here are 2 problems:
a) i can't use the counter 'n' and parfor doesn't allow me to use the index from the earlier loop :O :( please tell me how should i use the parfor to search position of the red reference value!!! :(
b) Also i used the preallocation of v matrix with zeros and at the end i want the final v matrix without zero i.e. with the values other than 0 only. v=(v(v~=0)) works and gives the v matrix in a column matrix. that is not what i want. :(
Please Help!
  1 个评论
Iain
Iain 2014-2-28
1. Put the parfor in the outer loop.
2. You can't use that n in a parfor loop. You may, instead, create "v" to be 2 by yb by zb, and initialised (or set) to NaN when you don't have a valid answer. You can then eliminate the NaNs by
v = reshape(v(~isnan(v),2,[]);
Your problem, however has a faster/simpler solution.
indices = find(b(:,:,1)>= rot_min & b(:,:,1)<=rot_max);
That gets you linear indices to find your qualifying values of b. ind2sub can then be used to return, into a couple of variables (or columns), the rows and column values of the qualifying elements.

请先登录,再进行评论。


Nitin
Nitin 2014-2-28
you need first to use:
matlabpool open, then use parfor
You will find some nice examples here

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by