How to use parfor for fast matrix calculations with different dimensions???
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Thomas
2014-2-26
编辑:Thomas
2014-2-26
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!