How do you do conditional assignment in arrays in Julia?

In Octave I can do

octave:1> A = [1 2; 3 4] A = 1 2 3 4 octave:2> A(A>1) -= 1 A = 1 1 2 3 

but in Julia the equivalent syntax does not work.

 julia> A = [1 2; 3 4] 2x2 Array{Int64,2}: 1 2 3 4 julia> A[A>1] -= 1 ERROR: `isless` has no method matching isless(::Int64, ::Array{Int64,2}) in > at operators.jl:33 

How do you conditionally assign values ​​to certain elements of a matrix or matrix in Julia?

+7
octave julia-lang
source share
1 answer

Your problem is not related to the purpose, in fact, that A > 1 itself does not work. Instead, you can use elementwise A .> 1 :

 julia> A = [1 2; 3 4]; julia> A .> 1 2x2 BitArray{2}: false true true true julia> A[A .> 1] -= 1000; julia> A 2x2 Array{Int32,2}: 1 -998 -997 -996 
+13
source share

All Articles