Fixed a bug when iterating over sparse input arrays