@@ -1599,14 +1599,14 @@ cat(n::Integer, x::Integer...) = reshape([x...], (ntuple(x->1, n-1)..., length(x
1599
1599
"""
1600
1600
findnext(A, i::Integer)
1601
1601
1602
- Find the next linear index >= `i` of a non-zero element of `A`, or `0` if not found.
1602
+ Find the next linear index >= `i` of a `true` element of `A`, or `0` if not found.
1603
1603
1604
1604
# Examples
1605
1605
```jldoctest
1606
- julia> A = [0 0; 1 0 ]
1607
- 2×2 Array{Int64 ,2}:
1608
- 0 0
1609
- 1 0
1606
+ julia> A = [false false; true false ]
1607
+ 2×2 Array{Bool ,2}:
1608
+ false false
1609
+ true false
1610
1610
1611
1611
julia> findnext(A,1)
1612
1612
2
@@ -1618,8 +1618,14 @@ julia> findnext(A,3)
1618
1618
function findnext (A, start:: Integer )
1619
1619
l = endof (A)
1620
1620
i = start
1621
+ warned = false
1621
1622
while i <= l
1622
- if A[i] != 0
1623
+ a = A[i]
1624
+ if ! warned && ! (a isa Bool)
1625
+ depwarn (" In the future `findnext` will only work on boolean collections. Use `findnext(x->x!=0, A)` instead." , :findnext )
1626
+ warned = true
1627
+ end
1628
+ if a != 0
1623
1629
return i
1624
1630
end
1625
1631
i = nextind (A, i)
@@ -1630,8 +1636,9 @@ end
1630
1636
"""
1631
1637
findfirst(A)
1632
1638
1633
- Return the linear index of the first non-zero value in `A` (determined by `A[i]!=0`) .
1639
+ Return the linear index of the first `true` value in `A`.
1634
1640
Returns `0` if no such value is found.
1641
+ To search for other kinds of values, pass a predicate as the first argument.
1635
1642
1636
1643
# Examples
1637
1644
```jldoctest
@@ -1649,58 +1656,6 @@ julia> findfirst(zeros(3))
1649
1656
"""
1650
1657
findfirst (A) = findnext (A, 1 )
1651
1658
1652
- """
1653
- findnext(A, v, i::Integer)
1654
-
1655
- Find the next linear index >= `i` of an element of `A` equal to `v` (using `==`), or `0` if not found.
1656
-
1657
- # Examples
1658
- ```jldoctest
1659
- julia> A = [1 4; 2 2]
1660
- 2×2 Array{Int64,2}:
1661
- 1 4
1662
- 2 2
1663
-
1664
- julia> findnext(A,4,4)
1665
- 0
1666
-
1667
- julia> findnext(A,4,3)
1668
- 3
1669
- ```
1670
- """
1671
- function findnext (A, v, start:: Integer )
1672
- l = endof (A)
1673
- i = start
1674
- while i <= l
1675
- if A[i] == v
1676
- return i
1677
- end
1678
- i = nextind (A, i)
1679
- end
1680
- return 0
1681
- end
1682
- """
1683
- findfirst(A, v)
1684
-
1685
- Return the linear index of the first element equal to `v` in `A`.
1686
- Returns `0` if `v` is not found.
1687
-
1688
- # Examples
1689
- ```jldoctest
1690
- julia> A = [4 6; 2 2]
1691
- 2×2 Array{Int64,2}:
1692
- 4 6
1693
- 2 2
1694
-
1695
- julia> findfirst(A,2)
1696
- 2
1697
-
1698
- julia> findfirst(A,3)
1699
- 0
1700
- ```
1701
- """
1702
- findfirst (A, v) = findnext (A, v, 1 )
1703
-
1704
1659
"""
1705
1660
findnext(predicate::Function, A, i::Integer)
1706
1661
@@ -1750,21 +1705,24 @@ julia> findfirst(iseven, A)
1750
1705
1751
1706
julia> findfirst(x -> x>10, A)
1752
1707
0
1708
+
1709
+ julia> findfirst(equalto(4), A)
1710
+ 3
1753
1711
```
1754
1712
"""
1755
1713
findfirst (testf:: Function , A) = findnext (testf, A, 1 )
1756
1714
1757
1715
"""
1758
1716
findprev(A, i::Integer)
1759
1717
1760
- Find the previous linear index <= `i` of a non-zero element of `A`, or `0` if not found.
1718
+ Find the previous linear index <= `i` of a `true` element of `A`, or `0` if not found.
1761
1719
1762
1720
# Examples
1763
1721
```jldoctest
1764
- julia> A = [0 0; 1 2 ]
1765
- 2×2 Array{Int64 ,2}:
1766
- 0 0
1767
- 1 2
1722
+ julia> A = [false false; true true ]
1723
+ 2×2 Array{Bool ,2}:
1724
+ false false
1725
+ true true
1768
1726
1769
1727
julia> findprev(A,2)
1770
1728
2
@@ -1775,8 +1733,14 @@ julia> findprev(A,1)
1775
1733
"""
1776
1734
function findprev (A, start:: Integer )
1777
1735
i = start
1736
+ warned = false
1778
1737
while i >= 1
1779
- A[i] != 0 && return i
1738
+ a = A[i]
1739
+ if ! warned && ! (a isa Bool)
1740
+ depwarn (" In the future `findprev` will only work on boolean collections. Use `findprev(x->x!=0, A)` instead." , :findprev )
1741
+ warned = true
1742
+ end
1743
+ a != 0 && return i
1780
1744
i = prevind (A, i)
1781
1745
end
1782
1746
return 0
@@ -1785,8 +1749,8 @@ end
1785
1749
"""
1786
1750
findlast(A)
1787
1751
1788
- Return the linear index of the last non-zero value in `A` (determined by `A[i]!=0`) .
1789
- Returns `0` if there is no non-zero value in `A`.
1752
+ Return the linear index of the last `true` value in `A`.
1753
+ Returns `0` if there is no `true` value in `A`.
1790
1754
1791
1755
# Examples
1792
1756
```jldoctest
@@ -1809,59 +1773,6 @@ julia> findlast(A)
1809
1773
"""
1810
1774
findlast (A) = findprev (A, endof (A))
1811
1775
1812
- """
1813
- findprev(A, v, i::Integer)
1814
-
1815
- Find the previous linear index <= `i` of an element of `A` equal to `v` (using `==`), or `0` if not found.
1816
-
1817
- # Examples
1818
- ```jldoctest
1819
- julia> A = [0 0; 1 2]
1820
- 2×2 Array{Int64,2}:
1821
- 0 0
1822
- 1 2
1823
-
1824
- julia> findprev(A, 1, 4)
1825
- 2
1826
-
1827
- julia> findprev(A, 1, 1)
1828
- 0
1829
- ```
1830
- """
1831
- function findprev (A, v, start:: Integer )
1832
- i = start
1833
- while i >= 1
1834
- A[i] == v && return i
1835
- i = prevind (A, i)
1836
- end
1837
- return 0
1838
- end
1839
-
1840
- """
1841
- findlast(A, v)
1842
-
1843
- Return the linear index of the last element equal to `v` in `A`.
1844
- Returns `0` if there is no element of `A` equal to `v`.
1845
-
1846
- # Examples
1847
- ```jldoctest
1848
- julia> A = [1 2; 2 1]
1849
- 2×2 Array{Int64,2}:
1850
- 1 2
1851
- 2 1
1852
-
1853
- julia> findlast(A,1)
1854
- 4
1855
-
1856
- julia> findlast(A,2)
1857
- 3
1858
-
1859
- julia> findlast(A,3)
1860
- 0
1861
- ```
1862
- """
1863
- findlast (A, v) = findprev (A, v, endof (A))
1864
-
1865
1776
"""
1866
1777
findprev(predicate::Function, A, i::Integer)
1867
1778
@@ -1921,16 +1832,23 @@ If there are no such elements of `A`, find returns an empty array.
1921
1832
1922
1833
# Examples
1923
1834
```jldoctest
1924
- julia> A = [1 2; 3 4]
1925
- 2×2 Array{Int64,2}:
1926
- 1 2
1927
- 3 4
1835
+ julia> A = [1 2 0 ; 3 4 0 ]
1836
+ 2×3 Array{Int64,2}:
1837
+ 1 2 0
1838
+ 3 4 0
1928
1839
1929
- julia> find(isodd,A)
1840
+ julia> find(isodd, A)
1930
1841
2-element Array{Int64,1}:
1931
1842
1
1932
1843
2
1933
1844
1845
+ julia> find(!iszero, A)
1846
+ 4-element Array{Int64,1}:
1847
+ 1
1848
+ 2
1849
+ 3
1850
+ 4
1851
+
1934
1852
julia> find(isodd, [2, 4])
1935
1853
0-element Array{Int64,1}
1936
1854
```
@@ -1955,9 +1873,8 @@ _index_remapper(iter) = OneTo(typemax(Int)) # safe for objects that don't imple
1955
1873
"""
1956
1874
find(A)
1957
1875
1958
- Return a vector of the linear indexes of the non-zeros in `A` (determined by `A[i]!=0`). A
1959
- common use of this is to convert a boolean array to an array of indexes of the `true`
1960
- elements. If there are no non-zero elements of `A`, `find` returns an empty array.
1876
+ Return a vector of the linear indices of the `true` values in `A`.
1877
+ To search for other kinds of values, pass a predicate as the first argument.
1961
1878
1962
1879
# Examples
1963
1880
```jldoctest
@@ -1971,7 +1888,7 @@ julia> find(A)
1971
1888
1
1972
1889
4
1973
1890
1974
- julia> find(zeros (3))
1891
+ julia> find(falses (3))
1975
1892
0-element Array{Int64,1}
1976
1893
```
1977
1894
"""
@@ -1980,7 +1897,12 @@ function find(A)
1980
1897
I = Vector {Int} (nnzA)
1981
1898
cnt = 1
1982
1899
inds = _index_remapper (A)
1900
+ warned = false
1983
1901
for (i,a) in enumerate (A)
1902
+ if ! warned && ! (a isa Bool)
1903
+ depwarn (" In the future `find(A)` will only work on boolean collections. Use `find(x->x!=0, A)` instead." , :find )
1904
+ warned = true
1905
+ end
1984
1906
if a != 0
1985
1907
I[cnt] = inds[i]
1986
1908
cnt += 1
@@ -1989,7 +1911,7 @@ function find(A)
1989
1911
return I
1990
1912
end
1991
1913
1992
- find (x:: Number ) = x == 0 ? Array {Int,1} (0 ) : [ 1 ]
1914
+ find (x:: Bool ) = x ? [ 1 ] : Array {Int,1} (0 )
1993
1915
find (testf:: Function , x:: Number ) = ! testf (x) ? Array {Int,1} (0 ) : [1 ]
1994
1916
1995
1917
findn (A:: AbstractVector ) = find (A)
0 commit comments