Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findnz does not work for arrays of arbitrary dimension #27849

Closed
cossio opened this issue Jun 28, 2018 · 4 comments
Closed

findnz does not work for arrays of arbitrary dimension #27849

cossio opened this issue Jun 28, 2018 · 4 comments
Labels
feature Indicates new feature / enhancement requests search & find The find* family of functions sparse Sparse arrays

Comments

@cossio
Copy link
Contributor

cossio commented Jun 28, 2018

It is only defined for matrices. But to me it seems reasonable to define findnz(A) for any higher-dimensional array. It would return (I1, I2, ..., Id, V), where d is the number of dimensions in A, Ii is the indices of non-zero entries along dimension i, and V is the value of those non-zero entries.

@mbauman mbauman added sparse Sparse arrays feature Indicates new feature / enhancement requests labels Jun 28, 2018
@cossio
Copy link
Contributor Author

cossio commented Jun 28, 2018

The implementation could be something like

function findnz2(A::AbstractArray{T, N}) where {T, N}
    nnzA = count(t -> t != 0, A)
    idxs = ntuple(_ -> zeros(Int, nnzA), N)
    V = Vector{T}(nnzA)
    cnt = 0
    for idx in eachindex(A)
        if A[idx]  0
            cnt += 1
            indexes::NTuple{N, Int} = ind2sub(A, idx)
            for d = 1 : N
                idxs[d][cnt] = indexes[d]
                V[cnt] = A[idx]
            end
        end
    end
    return (idxs..., V)
end

@nalimilan
Copy link
Member

findnz has been moved to SparseMatrices since it's mainly designed for this case. Use findall(iszero, x) instead for general arrays. On 0.7, it returns cartesian indices.

@nalimilan nalimilan added the search & find The find* family of functions label Jun 28, 2018
@cossio cossio closed this as completed Jun 28, 2018
@mbauman
Copy link
Sponsor Member

mbauman commented Jun 28, 2018

Note that findnz on dense matrices is a strange operation — and one that we had been considering deprecating (#24910 and #25641). It's really X <: SparseArray ? findstored(X) : find(!iszero, X).

Unfortunately that discussion had gotten closed and forgotten without fully resolving the deprecation.

@cossio
Copy link
Contributor Author

cossio commented Jun 28, 2018

I guess it's better to deprecate it then, before 0.7 is out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Indicates new feature / enhancement requests search & find The find* family of functions sparse Sparse arrays
Projects
None yet
Development

No branches or pull requests

3 participants