Remove for loops as far as possible by using apply functions
Remove for loops as far as possible by using apply functions