This can be turned into a reasonably well vectorised operation using indexing. I'd be happy to put together an example if you like.