replace for loops with meshgrid trick, and filter out uninteresting region using logical indexing. much faster after this changes being made.