Usage of if inside arrays

Hello,

I wish to change values inside an array if certain criteria are met. Within the array I am focusing on the values in three sections like so:

image

(lets pretend it’s 10x10)

What I want is to check the values across all columns against specific rows, and if the value in those rows are higher or lower than a threshold, the value in that box is changed to either 2,3 or 4.

function create_trophic_test2()
    trophic_test2 = rand(10,10)
    if any(trophic_test2[1:3,:] .< 0.4) .&& any(trophic_test2[4:end,:] .> 0.2)
        trophic_test2 .=2
    elseif any(trophic_test2[:,4:7] .> 0.2) 
        trophic_test2 .=3
    else 
        any(trophic_test2[1:3,:] .> 0.2) .&& any(trophic_test2[4:end,:] .< 0.4)
        trophic_test2 .=4
    end
    return trophic_test2
end

trophic_test2 = create_trophic_test2

I haven’t looked closely at what you’re trying to accomplish, but here are a couple notes on spots in your code that probably aren’t doing what you expect:

The first line isn’t necessary here. It creates an array and binds it to trophic_test2, but then you immediately create an entirely different array and overwrite that binding. Also, instead of using Uniform from Distributions, you can just say rand(10, 10) which automatically draws from [0,1).

The else clause does not take a logical predicate as an argument. This syntax is equivalent to:

else
    trophic_test2[1:3,:] .> 0.2 && trophic_test2[4:end,:] .< 0.4

if needs a single Bool argument rather than an array of Bools. You probably want to wrap this in something like any or all. Also, you probably want to broadcast the && as well (.&&).

1 Like

Thanks for the tips! I have edited my example code.

Unfortunately this generates the error syntax: invalid syntax &(any(materialize(broadcasted(>, getindex(trophic_test2, :(4, lastindex(trophic_test2, 1)), :), 0.2)))) top-level scope at untitled-89f41a4f54dc03f5ad7a90ba69cb3b29:1

Without checking the logic it should be:

julia> function trophic_test2()
           trophic_test2 = rand(10,10)
           if any(trophic_test2[1:3,:] .< 0.4) && any(trophic_test2[4:end,:] .> 0.2)
               trophic_test2 .=2
           elseif any(trophic_test2[:,4:7] .> 0.2)
               trophic_test2 .=3
           else
                     trophic_test2 .=4
           end
           return trophic_test2
       end

Your syntax error is, because you added any together with the broadcast .&& like

any( ... ) .&& any( ... )

which gives an invalid syntax as any() returns a bool. You can not broadcast two bools, just arrays of bools, thats what broadcasting is for: arrays!

The line after else

else 
        any(trophic_test2[1:3,:] .> 0.2) .&& any(trophic_test2[4:end,:] .< 0.4)

does nothing.
Maybe you mean

elseif any(trophic_test2[1:3,:] .> 0.2) .&& any(trophic_test2[4:end,:] .< 0.4)
    trophic_test2 .=4
end

but this depends on your logic.

This has nothing to do with any. It’s just that there’s no such thing as .&& in Julia, so it’s parsed as .& &, which later errors:

julia> Meta.parse("1 .&& 2")
:(1 .& &2)

The short-circuiting operators && and || cannot be broadcasted (you can broadcast the bitwise operator & to do element-wise “and”, but it’s not needed here).

Also, here

You’re probably trying to call the function, but you’re not. This just binds the name trophic_test2 to also refer to the same function. To call the function, you need parentheses: create_trophic_test2().

This seems like one of those cases where if you just write a loop it will be both simpler and faster, especially if you arrange your code so that in a single pass over the array contents you check all of your criteria.

2 Likes

Edit: I have edited my code from the original posting

Hello,

I wish to change values inside an array if certain criteria are met. Within the array I am focusing on the values in three sections like so:

image

Green = plants
Blue = omnivores
Red = carnivores
White = zeros

What I want is to check the sum of a part of each column, and if criteria is met (sum = 0), change all the values in that column to a certain value.

However I end up overwriting all the values in the array, instead of just the columns that meet the criteria. Is there way to do this with if statements?

MWE

plants = 5
animals = 10

foodweb = rand(plants+animals,plants+animals)

foodweb
function create_trophic()
   trophic = copy(foodweb)

   trophic[plants+1:plants+animals,plants+1:plants+4] .= 0.0 # this is just to mimic my actual array
   trophic[1:plants,plants+7:plants+animals] .= 0.0 # this is just to mimic my actual array

   trophic[:,1:plants] .= 1.0
   if any(sum(trophic[plants+1:end,plants+1:end], dims=1) .== 0.0)
      trophic[:,plants+1:end] .= 2.0 #indicates herbivory
   elseif any(sum(trophic[1:plants,plants+1:end], dims=1) .== 0.0)
      trophic[:,plants+1:end] .= 4.0 #indicates carnivory
   else
      trophic[:,plants+1:end] .= 3.0 # indicates omnivory, 
   end
   return trophic
end

trophic = create_trophic()

Somehow I am twisting my thoughts but I admit I don’t understand what exactly you try to do.
Can we start with smaller array, e.g.:

julia> plants = 2
2

julia> animals = 3
3

julia> foodweb = rand(plants+animals,plants+animals)
5×5 Array{Float64,2}:
 0.788009  0.102808   0.0477518  0.0276722  0.17792
 0.593425  0.0217724  0.28276    0.461792   0.660652
 0.523148  0.815514   0.215802   0.31951    0.00259442
 0.51613   0.897586   0.663421   0.557986   0.419304
 0.823085  0.828389   0.633237   0.706203   0.914497

julia> foodweb[plants+1:plants+animals,plants+1:plants+1] .= 0.0
3×1 view(::Array{Float64,2}, 3:5, 3:3) with eltype Float64:
 0.0
 0.0
 0.0

julia> foodweb[1:plants,plants+2:plants+animals] .= 0.0
2×2 view(::Array{Float64,2}, 1:2, 4:5) with eltype Float64:
 0.0  0.0
 0.0  0.0

julia> foodweb[:,1:plants] .= 1.0
5×2 view(::Array{Float64,2}, :, 1:2) with eltype Float64:
 1.0  1.0
 1.0  1.0
 1.0  1.0
 1.0  1.0
 1.0  1.0

julia> foodweb
5×5 Array{Float64,2}:
 1.0  1.0  0.0477518  0.0       0.0
 1.0  1.0  0.28276    0.0       0.0
 1.0  1.0  0.0        0.31951   0.00259442
 1.0  1.0  0.0        0.557986  0.419304
 1.0  1.0  0.0        0.706203  0.914497

Ok, the values 1.0 in this array seem be related to your blue section, the other non zero values to green and red. But it is not the same order (as it is in your own example above). You may change this in your answer to what it really is but let it be small.

Now, how should the result array trophic look like?

You may describe your algorithm you have in mind with some kind of pseudo code, e.g.:

  1. check if in column 1 is at least one number not zero
  2. if yes change values in this column and in rows x to y to value z
  3. goto 1) and check next column

or something like that.

After some more twisting I give it try, and you can see, if this comes near your desired result:

julia> plants = 5
5

julia> animals = 10
10

julia>

julia> foodweb = rand(plants+animals,plants+animals)
15×15 Array{Float64,2}:
 0.790765   0.74945    0.178216  0.880243   0.0535738   0.0578965  0.974428   …  0.208304  0.720289   0.648201    0.737452   0.0847284   0.564161
 0.8582     0.46515    0.735909  0.208577   0.873841    0.527551   0.0390977     0.936396  0.129987   0.853694    0.23191    0.880875    0.399355
 0.0587578  0.317092   0.346076  0.272996   0.160502    0.473623   0.139277      0.9657    0.625101   0.442957    0.546243   0.281135    0.867061
 0.669335   0.23564    0.154817  0.453713   0.366344    0.537439   0.358694      0.990022  0.0286653  0.779038    0.536321   0.787731    0.596143
 0.659701   0.152065   0.856429  0.279055   0.384945    0.957591   0.341439      0.23478   0.466294   0.227259    0.701546   0.837532    0.893488
 0.0477922  0.935403   0.164517  0.698783   0.540316    0.699417   0.68533    …  0.990392  0.677236   0.178252    0.47287    0.601711    0.648389
 0.364394   0.0506907  0.951289  0.0418122  0.305796    0.626786   0.0393663     0.149337  0.545259   0.031105    0.0976091  0.58886     0.202296
 0.426242   0.923585   0.739397  0.276032   0.230438    0.887925   0.267237      0.308956  0.110004   0.218       0.752419   0.362512    0.808582
 0.632486   0.710518   0.488124  0.316211   0.431615    0.590922   0.0856947     0.845271  0.114726   0.991982    0.592431   0.396041    0.577046
 0.265683   0.669511   0.642308  0.7515     0.295317    0.74549    0.201287      0.145904  0.0425054  0.433327    0.67357    0.45051     0.374685
 0.993715   0.491474   0.970538  0.578483   0.953251    0.470365   0.514761   …  0.333113  0.613621   0.00758787  0.0229881  0.715957    0.913001
 0.70124    0.415128   0.291898  0.989158   0.00726582  0.0460169  0.201821      0.294963  0.909659   0.673933    0.654697   0.401348    0.199975
 0.358803   0.669853   0.91429   0.48196    0.0323081   0.4207     0.760229      0.664042  0.753911   0.452759    0.595092   0.00637201  0.927855
 0.436788   0.428121   0.515393  0.913713   0.257735    0.5943     0.17623       0.808202  0.289646   0.552934    0.401224   0.455273    0.367444
 0.330589   0.0320683  0.115222  0.494594   0.568123    0.774907   0.249329      0.64501   0.190103   0.230544    0.89837    0.0482878   0.67141

julia>

julia> foodweb
15×15 Array{Float64,2}:
 0.790765   0.74945    0.178216  0.880243   0.0535738   0.0578965  0.974428   …  0.208304  0.720289   0.648201    0.737452   0.0847284   0.564161
 0.8582     0.46515    0.735909  0.208577   0.873841    0.527551   0.0390977     0.936396  0.129987   0.853694    0.23191    0.880875    0.399355
 0.0587578  0.317092   0.346076  0.272996   0.160502    0.473623   0.139277      0.9657    0.625101   0.442957    0.546243   0.281135    0.867061
 0.669335   0.23564    0.154817  0.453713   0.366344    0.537439   0.358694      0.990022  0.0286653  0.779038    0.536321   0.787731    0.596143
 0.659701   0.152065   0.856429  0.279055   0.384945    0.957591   0.341439      0.23478   0.466294   0.227259    0.701546   0.837532    0.893488
 0.0477922  0.935403   0.164517  0.698783   0.540316    0.699417   0.68533    …  0.990392  0.677236   0.178252    0.47287    0.601711    0.648389
 0.364394   0.0506907  0.951289  0.0418122  0.305796    0.626786   0.0393663     0.149337  0.545259   0.031105    0.0976091  0.58886     0.202296
 0.426242   0.923585   0.739397  0.276032   0.230438    0.887925   0.267237      0.308956  0.110004   0.218       0.752419   0.362512    0.808582
 0.632486   0.710518   0.488124  0.316211   0.431615    0.590922   0.0856947     0.845271  0.114726   0.991982    0.592431   0.396041    0.577046
 0.265683   0.669511   0.642308  0.7515     0.295317    0.74549    0.201287      0.145904  0.0425054  0.433327    0.67357    0.45051     0.374685
 0.993715   0.491474   0.970538  0.578483   0.953251    0.470365   0.514761   …  0.333113  0.613621   0.00758787  0.0229881  0.715957    0.913001
 0.70124    0.415128   0.291898  0.989158   0.00726582  0.0460169  0.201821      0.294963  0.909659   0.673933    0.654697   0.401348    0.199975
 0.358803   0.669853   0.91429   0.48196    0.0323081   0.4207     0.760229      0.664042  0.753911   0.452759    0.595092   0.00637201  0.927855
 0.436788   0.428121   0.515393  0.913713   0.257735    0.5943     0.17623       0.808202  0.289646   0.552934    0.401224   0.455273    0.367444
 0.330589   0.0320683  0.115222  0.494594   0.568123    0.774907   0.249329      0.64501   0.190103   0.230544    0.89837    0.0482878   0.67141

julia> function create_trophic()
          trophic = copy(foodweb)

          trophic[plants+1:plants+animals,plants+1:plants+4] .= 0.0 # this is just to mimic my actual array
          trophic[1:plants,plants+7:plants+animals] .= 0.0 # this is just to mimic my actual array

          trophic[:,1:plants] .= 1.0

          for column in 1:plants+animals
              if all(trophic[:,column] .>= 1.0)
                 trophic[:,column] .= 1.0 #indicates herbivory
              elseif all(trophic[1:plants,column] .> 0.0) && all(trophic[plants+1:end,column] .== 0.0)
                 trophic[1:plants,column] .= 4.0 #indicates carnivory
              elseif all(trophic[1:plants,column] .== 0.0) && all(trophic[plants+1:end,column] .> 0.0)
                 trophic[plants+1:end,column] .= 3.0 # indicates omnivory,
              else
                 trophic[:,column] .= 0.0
              end
          end
          return trophic
       end
create_trophic (generic function with 1 method)

julia>

julia> trophic = create_trophic()
15×15 Array{Float64,2}:
 1.0  1.0  1.0  1.0  1.0  4.0  4.0  4.0  4.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  1.0  1.0  4.0  4.0  4.0  4.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  1.0  1.0  4.0  4.0  4.0  4.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  1.0  1.0  4.0  4.0  4.0  4.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  1.0  1.0  4.0  4.0  4.0  4.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  3.0  3.0  3.0  3.0

julia> 

Or another variant (because you example matrix trophic does not really match your image):

julia> plants = 5
5

julia> animals = 10
10

julia>

julia> foodweb = rand(plants+animals,plants+animals)
15×15 Array{Float64,2}:
 0.586139   0.242526   0.175424   0.41908   0.608365   0.623958   0.985096   …  0.564946   0.563235   0.855666   0.160699   0.294043    0.439596
 0.606276   0.783888   0.187657   0.45819   0.382996   0.538697   0.416667      0.0788532  0.22072    0.237465   0.65115    0.263006    0.260589
 0.121644   0.74934    0.176507   0.159718  0.0899451  0.998951   0.98093       0.18824    0.990353   0.245849   0.211564   0.192811    0.419919
 0.254097   0.0542392  0.528426   0.738465  0.564184   0.163758   0.533865      0.278683   0.509338   0.357289   0.527181   0.495044    0.612462
 0.113537   0.0302919  0.361624   0.275809  0.186019   0.120429   0.0733641     0.484558   0.882517   0.613164   0.834181   0.981004    0.445259
 0.360844   0.0184298  0.734411   0.457512  0.790891   0.11065    0.962243   …  0.676564   0.240609   0.546232   0.760843   0.676749    0.938689
 0.429256   0.337486   0.589287   0.866082  0.624502   0.908152   0.726356      0.999499   0.969522   0.166621   0.11203    0.458421    0.770571
 0.0345678  0.900259   0.0766377  0.892703  0.749685   0.699663   0.553896      0.0148111  0.873471   0.863698   0.43959    0.243636    0.192125
 0.184911   0.116365   0.546452   0.166702  0.902927   0.265729   0.199871      0.080607   0.497961   0.441525   0.761042   0.702948    0.972435
 0.343261   0.447473   0.318871   0.731504  0.807301   0.0286925  0.81666       0.567394   0.494868   0.0569126  0.241459   0.567982    0.411521
 0.398649   0.856336   0.564759   0.235559  0.0664199  0.836829   0.326462   …  0.628911   0.0471175  0.600328   0.391617   0.726427    0.0038903
 0.570695   0.934198   0.23961    0.405866  0.766589   0.360281   0.53942       0.339671   0.780902   0.0209611  0.0289423  0.00133611  0.102266
 0.473447   0.169119   0.399808   0.532643  0.825839   0.513232   0.169403      0.748695   0.745113   0.0690881  0.807541   0.0278596   0.502701
 0.549272   0.72991    0.892347   0.706628  0.516569   0.388774   0.168778      0.542948   0.25892    0.483741   0.274832   0.732203    0.904545
 0.598189   0.805712   0.864001   0.551519  0.648825   0.344549   0.168836      0.697156   0.688419   0.653445   0.599199   0.822995    0.181469

julia>

julia> foodweb
15×15 Array{Float64,2}:
 0.586139   0.242526   0.175424   0.41908   0.608365   0.623958   0.985096   …  0.564946   0.563235   0.855666   0.160699   0.294043    0.439596
 0.606276   0.783888   0.187657   0.45819   0.382996   0.538697   0.416667      0.0788532  0.22072    0.237465   0.65115    0.263006    0.260589
 0.121644   0.74934    0.176507   0.159718  0.0899451  0.998951   0.98093       0.18824    0.990353   0.245849   0.211564   0.192811    0.419919
 0.254097   0.0542392  0.528426   0.738465  0.564184   0.163758   0.533865      0.278683   0.509338   0.357289   0.527181   0.495044    0.612462
 0.113537   0.0302919  0.361624   0.275809  0.186019   0.120429   0.0733641     0.484558   0.882517   0.613164   0.834181   0.981004    0.445259
 0.360844   0.0184298  0.734411   0.457512  0.790891   0.11065    0.962243   …  0.676564   0.240609   0.546232   0.760843   0.676749    0.938689
 0.429256   0.337486   0.589287   0.866082  0.624502   0.908152   0.726356      0.999499   0.969522   0.166621   0.11203    0.458421    0.770571
 0.0345678  0.900259   0.0766377  0.892703  0.749685   0.699663   0.553896      0.0148111  0.873471   0.863698   0.43959    0.243636    0.192125
 0.184911   0.116365   0.546452   0.166702  0.902927   0.265729   0.199871      0.080607   0.497961   0.441525   0.761042   0.702948    0.972435
 0.343261   0.447473   0.318871   0.731504  0.807301   0.0286925  0.81666       0.567394   0.494868   0.0569126  0.241459   0.567982    0.411521
 0.398649   0.856336   0.564759   0.235559  0.0664199  0.836829   0.326462   …  0.628911   0.0471175  0.600328   0.391617   0.726427    0.0038903
 0.570695   0.934198   0.23961    0.405866  0.766589   0.360281   0.53942       0.339671   0.780902   0.0209611  0.0289423  0.00133611  0.102266
 0.473447   0.169119   0.399808   0.532643  0.825839   0.513232   0.169403      0.748695   0.745113   0.0690881  0.807541   0.0278596   0.502701
 0.549272   0.72991    0.892347   0.706628  0.516569   0.388774   0.168778      0.542948   0.25892    0.483741   0.274832   0.732203    0.904545
 0.598189   0.805712   0.864001   0.551519  0.648825   0.344549   0.168836      0.697156   0.688419   0.653445   0.599199   0.822995    0.181469

julia> function create_trophic()
          trophic = copy(foodweb)

          trophic[plants+1:plants+animals,plants+1:plants+4] .= 0.0 # this is just to mimic my actual array
          trophic[1:plants,plants+7:plants+animals] .= 0.0 # this is just to mimic my actual array

          trophic[:,1:plants] .= 1.0

          for column in 1:plants+animals
              if all(trophic[:,column] .> 0.0)
                 trophic[:,column] .= 1.0 #indicates herbivory
              elseif all(trophic[1:plants,column] .> 0.0) && all(trophic[plants+1:end,column] .== 0.0)
                 trophic[1:plants,column] .= 4.0 #indicates carnivory
              elseif all(trophic[1:plants,column] .== 0.0) && all(trophic[plants+1:end,column] .> 0.0)
                 trophic[plants+1:end,column] .= 3.0 # indicates omnivory,
              end
          end
          return trophic
       end
create_trophic (generic function with 1 method)

julia>

julia> trophic = create_trophic()
15×15 Array{Float64,2}:
 1.0  1.0  1.0  1.0  1.0  4.0  4.0  4.0  4.0  1.0  1.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  1.0  1.0  4.0  4.0  4.0  4.0  1.0  1.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  1.0  1.0  4.0  4.0  4.0  4.0  1.0  1.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  1.0  1.0  4.0  4.0  4.0  4.0  1.0  1.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  1.0  1.0  4.0  4.0  4.0  4.0  1.0  1.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  1.0  1.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  1.0  1.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  1.0  1.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  1.0  1.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  1.0  1.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  1.0  1.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  1.0  1.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  1.0  1.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  1.0  1.0  3.0  3.0  3.0  3.0
 1.0  1.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  1.0  1.0  3.0  3.0  3.0  3.0

julia>