What does this mean? findnz(A::AbstractMatrix)` is deprecated, use `begin

I am having trouble understanding this:

**Warning:**findnz(A::AbstractMatrix)is deprecated, usebegin
I = findall(!iszero, A)
(getindex.(I, 1), getindex.(I, 2), A[I])
endinstead. **│**

What does it mean?
Thanks

It is telling you to write

I = findall(!iszero, A)
getindex.(I, 1), getindex.(I, 2), A[I]

instead of findnz because this will give you the exact output that findnz used to do.

julia> A = rand(0:1, 2, 2)
2×2 Array{Int64,2}:
 0  0
 1  1

julia> I = findall(!iszero, A)
2-element Array{CartesianIndex{2},1}:
 CartesianIndex(2, 1)
 CartesianIndex(2, 2)

julia> getindex.(I, 1), getindex.(I, 2), A[I]
([2, 2], [1, 2], [1, 1])

However, it is rarely needed to do this destructuring of I.

2 Likes

Got it! It was the begin and end bits that threw me. I have findnz sprayed through out my code base. I am tempted to simply implement my own findnz using the new replacement code from above. Nevertheless, findnz must have been deprecated for a reason. Would rolling my own be ill advised.

You can read a little bit of background at https://github.com/JuliaLang/julia/pull/27869.

You can always pack up the code above in your own findnz function and at some later point think about if you want to refactor your code to not need it.

I think it would be nice to have a Base.@deprecate version that allows you to provide a bit more information. Right now, either you use Base.@deprecate, which is convenient but can result in somewhat convoluted replacement suggestions like this, or you manually use Base.depwarn, which has the opposite problem.