Skip to content

Commit

Permalink
Generalize findn and findnz for non-1 indices
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Aug 4, 2016
1 parent 06c6d59 commit 37e9c5c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ function findn(A::AbstractMatrix)
I = similar(A, Int, nnzA)
J = similar(A, Int, nnzA)
count = 1
for j=1:size(A,2), i=1:size(A,1)
for j=indices(A,2), i=indices(A,1)
if A[i,j] != 0
I[count] = i
J[count] = j
Expand All @@ -811,7 +811,7 @@ function findnz{T}(A::AbstractMatrix{T})
NZs = Array{T,1}(nnzA)
count = 1
if nnzA > 0
for j=1:size(A,2), i=1:size(A,1)
for j=indices(A,2), i=indices(A,1)
Aij = A[i,j]
if Aij != 0
I[count] = i
Expand Down
8 changes: 8 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,14 @@ pmax, ipmax = findmax(parent(A))
@test amax == pmax
@test A[iamax] == amax
@test amax == parent(A)[ipmax]
z = OffsetArray([0 0; 2 0; 0 0; 0 0], (-3,-1))
I,J = findn(z)
@test I == [-1]
@test J == [0]
I,J,N = findnz(z)
@test I == [-1]
@test J == [0]
@test N == [2]

v = OffsetArray([1,1e100,1,-1e100], (-3,))*1000
v2 = OffsetArray([1,-1e100,1,1e100], (5,))*1000
Expand Down

0 comments on commit 37e9c5c

Please sign in to comment.