Replacing loop by a couple of numpy expressions.