How to use parfor for fast matrix calculations with different dimensions???

4 views (last 30 days)
Here is the code that i wrote to find 1 in the matrix with general for-loop:
b=[0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 1 1 0 0 0 0 0; 0 0 1 1 1 1 1 0 0 0; 0 0 0 1 1 1 0 0 0 0; 0 0 0 1 1 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0]; n=0; s=size(b,1); %rows t=size(b,2); %columns v = zeros(s*t,2); for g=1:s for h=1:t if b(g,h)== 1 n=n+1; v(n,:)=[g h;]; end end end v v=(v(v~=0))
here are 2 problems:
a) i can't use the counter 'n', so i made a formula n=col+(row-1)*col. However 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 1!!! :(
b) 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!

Answers (2)

Thomas
Thomas on 26 Feb 2014
Edited: Thomas on 26 Feb 2014
You do not need to use 'parfor' or even 'for' to get to what you are trying (i.e. find 1's in the matrix). Is there a specific requirement to use 'parfor' or 'for'?
this single line of code does everything your code does and I'm sure is faster than a parfor..
[row,col]=find(b==1);
out=[row col] % is you want it as a matrix
If you definitely want to use parfor try reading the Reduction Assignments: http://www.mathworks.com/help/distcomp/getting-started-with-parfor.html#brdqn6p-1

Mandar
Mandar on 26 Feb 2014
Actually i have wrote down 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...

Categories

Find more on Parallel Computing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!