# How could we add elements in a new array? and how could we use "if" with mutiple conditions?

**URL:** <https://discourse.julialang.org/t/how-could-we-add-elements-in-a-new-array-and-how-could-we-use-if-with-mutiple-conditions/64700>\
**Category:** New to Julia\
**Created:** [July 15, 2021, 3:42pm UTC](https://discourse.julialang.org/t/how-could-we-add-elements-in-a-new-array-and-how-could-we-use-if-with-mutiple-conditions/64700 "2021-07-15T15:42:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.julialang.org/u/Cyber)\
**Post date:** [July 15, 2021, 3:42pm UTC](https://discourse.julialang.org/t/how-could-we-add-elements-in-a-new-array-and-how-could-we-use-if-with-mutiple-conditions/64700/1 "2021-07-15T15:42:58Z")

</div>

Hello all,

In matlab, we could use X = to define an empty array and then add elements in X like X[1] = 2 or X[i] = rand in a for loop and does not require us to know the size of X in advance.

I was wondering how could I do this in Julia? Here is my code,Coo is a 500-element Vector{Vector{Float64}}.

```julia
using Plots
using JLD
using StaticArrays
Coo = load("data.jld")["data"] 
x = load("data2.jld")["data"] 
y = load("data3.jld")["data"] 
gx = []
for i = 1:length(Coo)
    if Coo[i][1] >4 & Coo[i][2] <4
        gx[i] = Coo[i][1]
    end
end

```

This line

```julia
Coo[i][1] >4 & Coo[i][2] <4

```

and this line here

```julia
gx[i] = Coo[i][1]

```

both failed and have problems. Sorry that I feel confused to debug them at this stage.  
Could you please give some suggestions or comment on this?  
Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 15, 2021, 3:54pm UTC](https://discourse.julialang.org/t/how-could-we-add-elements-in-a-new-array-and-how-could-we-use-if-with-mutiple-conditions/64700/2 "2021-07-15T15:54:47Z")

</div>

To answer half, you want `&&` instead of `&`.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 15, 2021, 4:08pm UTC](https://discourse.julialang.org/t/how-could-we-add-elements-in-a-new-array-and-how-could-we-use-if-with-mutiple-conditions/64700/3 "2021-07-15T16:08:50Z")

</div>

For the other half of your question, you want `push!`

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 15, 2021, 4:17pm UTC](https://discourse.julialang.org/t/how-could-we-add-elements-in-a-new-array-and-how-could-we-use-if-with-mutiple-conditions/64700/4 "2021-07-15T16:17:17Z")

</div>

To expand on this answer:

```julia
help?> &                                    
search: & &&                                
                                            
  x & y                                     
                                            
  Bitwise and. Implements three-valued logic [...]

help?> &&                                                                      
search: &&                                                                     
                                                                               
  x && y                                                                       
                                                                               
  Short-circuiting boolean AND.                                                
                                                                               
  See also &, the ternary operator ? :, and the manual section on control flow.

```

If you enter `?` in the REPL and enter a symbol, function name, operator etc., you’ll get this sort of documentation. Most packages and almost everything in julia Base has this sort of documentation available.

In general, I’d recommend checking out the manual (especially the section about [differences from Matlab](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-MATLAB)) if you’re new to the language.

---

<div class="post-metadata">

**Author:** ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.julialang.org/u/Cyber)\
**Post date:** [July 15, 2021, 4:19pm UTC](https://discourse.julialang.org/t/how-could-we-add-elements-in-a-new-array-and-how-could-we-use-if-with-mutiple-conditions/64700/5 "2021-07-15T16:19:30Z")

</div>

Thank you very much all !  
It is really helpful and now I know how to seek helps from Julia documentation!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 15, 2021, 4:41pm UTC](https://discourse.julialang.org/t/how-could-we-add-elements-in-a-new-array-and-how-could-we-use-if-with-mutiple-conditions/64700/6 "2021-07-15T16:41:10Z")

</div>

> [@Cyber](#):
>
> `gx = []`

Note that this is an _anti-pattern_ in Julia, because it creates an array of elements of `Any` type:

```julia
julia> x = []
Any[]

```

this is very bad for performance. You should always (when possible) specify the type of data that the vector will contain:

```julia
julia> x = Float64[]
Float64[]

```

By the way, another anti-pattern in Julia is to heavy computations outside functions. In that particular example, you should do

```julia
function update!(gx,Coo)
  for i = 1:length(Coo)
      if Coo[i][1] >4 & Coo[i][2] <4
          gx[i] = Coo[i][1]
      end
  end
end

```

and call this with `update!(gx,Coo)`.

---

<div class="post-metadata">

**Author:** ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.julialang.org/u/Cyber)\
**Post date:** [July 15, 2021, 4:57pm UTC](https://discourse.julialang.org/t/how-could-we-add-elements-in-a-new-array-and-how-could-we-use-if-with-mutiple-conditions/64700/7 "2021-07-15T16:57:22Z")

</div>

Amazing! Really appreciate these helps!
