fixed problem in the sparse matrix handling reported by Thouis (Ray) Jones

this is alternative fix for https://github.com/Itseez/opencv/pull/495