Prev: deferred length character as subroutine argument
Next: How to define a user data type containing an array with dimensions decided in running time
From: Ed Stein8778 on 15 May 2010 04:09 Hello, I'm looking for a efficient, vectorized Fortran90/95 code that works like Matlab's accumarray function, which accumulates elements of an input (data) array, based on subscript arrays (of the same shape as the data array) which point to the location in a new target array that the input array are to accumulate into. More precisely: Given a data matrix A sized (M,N), and a set of row indexes DROW(M,N) and column indexes DCOLUMN(M,N), the accumarray function accumulates data from A into a new array D sized (Md,Nd) where D(md,nd) is the sum of A(m,n) for all values of m,n for which DROW(m,n)=md and DCOLUMN(m,n)=nd. Matlab's accumarray does this: D = accumarray([DROW(:) DCOLUMN(:)], A(:) ); Can anyone suggest some Fortran code equivalent that avoids loops? Though Matlab's accumarray handles arbitrary rank, restricting the Fortran version to 2-d arrays A and D as in the above example is fine. Thanks very much, Ed
From: dpb on 15 May 2010 05:15
Ed Stein8778 wrote: > Hello, > > I'm looking for a efficient, vectorized Fortran90/95 code that works like > Matlab's accumarray function, which accumulates elements of an input > (data) array, based on subscript arrays (of the same shape as the data > array) which point to the location in a new target array that the input > array are to accumulate into. More precisely: > > Given a data matrix A sized (M,N), and a set of row indexes DROW(M,N) and > column indexes DCOLUMN(M,N), the accumarray function accumulates data > from A into a new array D sized (Md,Nd) where D(md,nd) is the sum of > A(m,n) for all values of m,n for which DROW(m,n)=md and DCOLUMN(m,n)=nd. > > Matlab's accumarray does this: > > D = accumarray([DROW(:) DCOLUMN(:)], A(:) ); > > Can anyone suggest some Fortran code equivalent that avoids loops? > Though Matlab's accumarray handles arbitrary rank, restricting the > Fortran version to 2-d arrays A and D as in the above example is fine. As in Matlab, vectorization in source code isn't necessarily always the answer. Just as you build blocks in Matlab in m-files that sometimes have loops, here I think the solution is to write a routine and hide the details, then call it. -- |