How to use parfor for fast matrix calculations with different dimensions???
2 views (last 30 days)
Show older comments
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!
0 Comments
Answers (2)
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
0 Comments
See Also
Categories
Find more on Parallel for-Loops (parfor) 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!