# Error in for loop

**URL:** <https://discourse.julialang.org/t/error-in-for-loop/89212>\
**Category:** New to Julia\
**Created:** [October 24, 2022, 9:33pm UTC](https://discourse.julialang.org/t/error-in-for-loop/89212 "2022-10-24T21:33:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)\
**Post date:** [October 24, 2022, 9:33pm UTC](https://discourse.julialang.org/t/error-in-for-loop/89212/1 "2022-10-24T21:33:29Z")

</div>

Hello all,

I am writing a simple code given as:

```julia
for i in 1:4
    b[i] = sum(M[i,j]*z[j] for j in items)+sum(N[i,j]*w[j] for j in items)+q[i]
end
print(b)

```

it is giving me error

```julia
BoundsError: attempt to access 0-element Vector{Any} at index [1]

```

Can anyone tell me whats going on.

Thanks

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [October 24, 2022, 9:40pm UTC](https://discourse.julialang.org/t/error-in-for-loop/89212/2 "2022-10-24T21:40:28Z")

</div>

One or the arrays you are trying to access is empty. Julia won’t automatically resize an array for you like MATLAB, you need to preallocate the memory first, or just make sure that you are accessing the memory within the bounds.

---

<div class="post-metadata">

**Author:** ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)\
**Post date:** [October 24, 2022, 9:43pm UTC](https://discourse.julialang.org/t/error-in-for-loop/89212/3 "2022-10-24T21:43:22Z")

</div>

still no idea whats going on ☹ b[i] is just a new vector that i am assigning to the values of right hand side. very simple code. the right hand side is working, when i try to assign the value it gives me error

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [October 24, 2022, 9:45pm UTC](https://discourse.julialang.org/t/error-in-for-loop/89212/4 "2022-10-24T21:45:16Z")

</div>

`@show b`

---

<div class="post-metadata">

**Author:** ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)\
**Post date:** [October 24, 2022, 9:53pm UTC](https://discourse.julialang.org/t/error-in-for-loop/89212/5 "2022-10-24T21:53:47Z")

</div>

yes its print b four times but i want to save them in a vector, cant i just save it like b[i]?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 24, 2022, 9:54pm UTC](https://discourse.julialang.org/t/error-in-for-loop/89212/6 "2022-10-24T21:54:17Z")

</div>

> [@Muhammad\_Nadeem](#):
>
> `b[i]` is just a new vector that i am assigning to the values of right hand side

Unlike Matlab, Julia does not automatically grow arrays when you assign to them. The following code:

```julia
b = [] # initialize to empty array
b[1] = 7
b[2] = 3

```

is perfectly valid in Matlab but will give an error in Julia. (Most languages will give you an error for something like this; ~~Julia~~ Matlab is the odd one out.)

You have at least three options:

1. Pre-allocate the array of the desired size and type, e.g. `b = Vector{Float64}(undef, 4)` to pre-allocate an array of 4 floats (with undefined/arbitrary values) that you can write into, or `b = zeros(4)` for an array of zeros.
2. For Matlab-like behavior of appending one element at a time, use `push!(b, someval)` to append `someval` to the end of `b`, increasing the length of `b`. (This is done efficiently under the hood, with minimal copying of data.)
3. Use `map`, broadcasting, or a comprehension to create the array while assigning its elements to a particular computation, e.g. `b = [sum(...)+q[i] for i in 1:4]`.

---

<div class="post-metadata">

**Author:** ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)\
**Post date:** [October 24, 2022, 9:59pm UTC](https://discourse.julialang.org/t/error-in-for-loop/89212/7 "2022-10-24T21:59:04Z")

</div>

> [@stevengj](#):
>
> `b = Vector{Float64}(undef, 4)`

Thank you so much, make sense now.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 24, 2022, 10:42pm UTC](https://discourse.julialang.org/t/error-in-for-loop/89212/8 "2022-10-24T22:42:40Z")

</div>

> [@stevengj](#):
>
> Julia is the odd one

I think you meant that Matlab is the odd one. I believe Lua also allows this, if I remember correctly.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 25, 2022, 12:33am UTC](https://discourse.julialang.org/t/error-in-for-loop/89212/9 "2022-10-25T00:33:35Z")

</div>

> [@Henrique\_Becker](#):
>
> I think you meant that Matlab is the odd one.

Yes, sorry.
