2015-04-11 7 views
7

In Octave, kann ichWie führen Sie bedingte Zuweisung in Arrays in Julia durch?

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

    1 2 
    3 4 

octave:2> A(A>1) -= 1 
A = 

    1 1 
    2 3 

aber in Julia, die äquivalente Syntax funktioniert nicht.

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 

Wie ordnen Sie bestimmten Array- oder Matrixelementen in Julia bedingte Werte zu?

Antwort

13

Ihr Problem ist nicht mit der Zuweisung, per se, ist es, dass A > 1 selbst nicht funktioniert. Sie können stattdessen das elementweise A .> 1 verwenden:

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