I implemented a version of a CFAR average in MATLAB but it's 'slow' and I need to speed it up. I included the pseudo code and logic below. I used a nested for loop to calculate the moving average but this is no good since I want to run this on hundreds of millions of sample points. Does anyone have any methods for making this run faster? As long as it doesn't use a nested for loop, anything is better. Any suggestions for using just one for loop?

This is the picture I used to calculate the lead and lag training windows. I am looping though all the points in my range window and calculating average lead and lag on the training samples for the cell under test CUT. For the CUT near the start or end of the range window, I used either the lead or lag and used double the training size. Again this is pseudo code so I don't care about saving off my values. Overwriting values is fine for now. Data is my MATLAB variable I am using to calculate the CFAR average.

Please let me know how to speed this up!


[![enter image description here][1]][1] [![enter image description here][2]][2]


For n=1:range_window_length
% Condition if CUT is less than G+T
     if n<G+T
         for i=n+G:n+G+T*2 %use 2 times T
% Condition if CUT is greater than than range window length minus (G+T)
     elseif n>range_window_length-(G+T)
         for i=n-G-2*T : n-G
% middle region calculate lead and lag
          for i=n-G-T:n-G
          for i=n+G:n+G+T


  [1]: https://i.stack.imgur.com/EQAJn.png
  [2]: https://i.stack.imgur.com/7cL72.png
  • 2
    $\begingroup$ Can you use filter(), conv() or movsum() functions? They are implemented as library functions using whatever low level programming is needed to make them fast. $\endgroup$
    – Knut Inge
    Aug 11, 2021 at 22:34

1 Answer 1


The easiest way to accelerate this would be to eliminate as many for-loops as you can and instead refactor to use array operations on your data. Check the help page for arrayfun() as well; it's a ton faster than looping if you can make use of it. Alternatively you might expand a matrix that stores the indexes you need in advance and using that to pull values out of data, then performing slicing operations on the expanded output and then a reduction step to accumulate the result you need. These aren't specific to the code you included, because I think it's a lot more important you are presented with the methodologies that create a more performant program. Loops inside loops easily create O(n^2) inefficiencies that can be resolved with any of these techniques and MATLAB's built-ins.

Also, I strongly recommend against using MATLAB if you need reliably high performance. While getting good performance is certainly possible, it's much easier in programs like Python (numpy is extremely high performance if used correctly). This would also be a good candidate for GPU acceleration if your data sizes are large.

  • $\begingroup$ MATLAB is generally faster than Python. $\endgroup$
    – Royi
    May 9, 2022 at 4:13
  • $\begingroup$ @Royi Source? I have used both extensively and have noticed the exact opposite, at least with NumPy. Without NumPy, yes standard Python lists are slower than MATLAB arrays $\endgroup$
    – Keegs
    May 10, 2022 at 11:52
  • $\begingroup$ Numpy is based on BLAS and LAPACK. MATLAB uses the fastest libraries for those on x86 world (Intel MKL). Numpy also use them (The Anaconda distribution). So per command there is no advantage for any. On non vectorized code MATLAB can use JIT engine while, at the moment, the CPython doesn't have. $\endgroup$
    – Royi
    May 10, 2022 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.