parfor loop??? making it faster how?

2 Ansichten (letzte 30 Tage)
Mandar
Mandar am 28 Feb. 2014
Bearbeitet: Matt J am 28 Feb. 2014
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...

Antworten (2)

Mandar
Mandar am 28 Feb. 2014
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 Kommentar
Iain
Iain am 28 Feb. 2014
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.

Melden Sie sich an, um zu kommentieren.


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

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by