User Tools

Site Tools


arraysortandsets

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
arraysortandsets [2018/12/11 01:57]
julia [Finding the First Array Element that passes a Test]
arraysortandsets [2018/12/27 17:20] (current)
Line 11: Line 11:
  
 ```juliarepl ```juliarepl
-julia> pkgchk( [ "​julia"​ => v"1.0.2", "​StatsBase"​ => v"​0.25.0"​ ] )+julia> pkgchk.( [ "​julia"​ => v"1.0.3", "​StatsBase"​ => v"​0.25.0"​ ] );
  
 ``` ```
Line 273: Line 273:
 ``` ```
  
-To find the first zero,+To find the first matching ​zero (or other value)you can do the obvious---or you can define your own "​customized"​ `isequal` function:
  
 ```juliarepl ```juliarepl
-julia> findfirst( x->(x!=0), [1,​0,​1] ​.== 0 )+julia> findfirst( x->(x==0), [1,0,1] )  ## obvious
 2 2
 +
 +julia> findfirst( x->​(x!=0),​ [1,0,1] .== 0 )  ## overly involved
 +2
 +
 +julia> isone= isequal(1) ##​ a useful function for finding 1's and not 1's
 +(::​Base.Fix2{typeof(isequal),​Float64}) (generic function with 1 method)
 +
 +julia> findfirst( isone, [3,2,1,5,6] ) ## a '​1'​ in the 3rd position
 +3
 +
 +julia> findfirst( !isone, [1,​1,​1,​1,​3,​1,​1] ) ## a non-'​1'​ in the 5th position
 +5
 +
 ``` ```
-FIXME (Andreas) Maybe introduce `isequal(0)` and the negation `!isequal(0)`. They are convenient as predicates.+
  
  
Line 367: Line 380:
 ### Removing all NaN and/or Missing Values ### Removing all NaN and/or Missing Values
  
 +`isnan`
  
 ```juliarepl ```juliarepl
 julia> v= [ 0.0, 2.0, NaN, 3.0, NaN ]; julia> v= [ 0.0, 2.0, NaN, 3.0, NaN ];
  
-julia> filter( x->​(!isnan(x)),​ v )+julia> filter( x->​(!isnan(x)),​ v ) # one way to filter out all NaN obs
 3-element Array{Float64,​1}:​ 3-element Array{Float64,​1}:​
  0.0  0.0
Line 377: Line 391:
  3.0  3.0
  
-julia> findall( x->​!isnan(x),​ v )+julia> filter( !isnan, v ) == v[ .! isnan.(v) ] ## two more ways to do the same 
 +true 
 + 
 +julia> findall( x->​!isnan(x),​ v ) ## find the index values of non NaNs
 3-element Array{Int64,​1}:​ 3-element Array{Int64,​1}:​
  1  1
Line 383: Line 400:
  4  4
  
-julia> v[ findall( x->​!isnan(x),​ v ) ]+julia> v[ findall( x->​!isnan(x),​ v ) ] ## so this again removes all NaN's
 3-element Array{Float64,​1}:​ 3-element Array{Float64,​1}:​
  0.0  0.0
  2.0  2.0
  3.0  3.0
 +
 +julia> ismissing.( [ 1.0, missing, 3.0 ] ) ## same trick for missing values
 +3-element BitArray{1}:​
 + false
 +  true
 + false
 +
 ``` ```
  
arraysortandsets.txt · Last modified: 2018/12/27 17:20 (external edit)