Using nditer would be the way to go for that, but it would add some overhead.