The variable `idx` here is a bit confusing. This is just because of the return values from `nnls`? Maybe you can put the logic (check whether this is `nnls`) here, instead of outside the loop. That additional check is going to be negligible in terms of its effect on performance.