1
$\begingroup$

I have an array farr, size Nx*Nx.

I have a function frk that maps index {n,m} to an integer (approximately Floor[Norm[{n,m}]])

I want to Bin all the elements in the array, according to their index and that function, and then sum them.

I am using this, but it is far too slow:

res = ConstantArray[0, 3*Nx]; Do[res[[frk[n,m,Nx] + 1]] += farr[[n, m]], {m, Nx}, {n, Nx}]; 

Any ideas?.. GatherBy seems almost right, but it cares about values, not indexes

$\endgroup$
5
  • $\begingroup$ What does the third argument of frk do? $\endgroup$ Commented Oct 18, 2014 at 13:50
  • $\begingroup$ @SjoerdC.deVries: It's the size of the array. $\endgroup$ Commented Oct 18, 2014 at 13:51
  • 1
    $\begingroup$ @DumpsterDoofus I know, but what does it do there? I mean, Oded defines frk informally as using two arguments and then calls it with three. $\endgroup$ Commented Oct 18, 2014 at 13:51
  • $\begingroup$ Hi, welcome to Mathematica.SE, please consider taking the tour so you learn the basic rules of the site. Please edit your question to improve it and make ir more clear what are you asking. $\endgroup$ Commented Oct 18, 2014 at 14:00
  • $\begingroup$ @SjoerdC.deVries it loops around using Nx - this is a discrete fourier transform, so if Nx=400, then "k=20" works for "n=20" and for n="380". The full thing: Floor[Norm[{Nx/2 - Abs[n - 1. - Nx/2], Nx/2 - Abs[m - 1. - Nx/2]}] $\endgroup$ Commented Oct 18, 2014 at 14:02

1 Answer 1

2
$\begingroup$

Solved it with this, works much much faster:

frkidx = 1 + GatherBy[Range[0, Nx*Nx - 1], frk[1 + Floor[#/Nx], 1 + Mod[#, Nx], Nx] &]; res = Map[Total[farr[[#]]] &, idx]; 

(I had to flatten farr)

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.