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!