# Boundserror: attempt to access 120 times 1 matrix{Float} at index \[1,3\]

**URL:** <https://discourse.julialang.org/t/boundserror-attempt-to-access-120-times-1-matrix-float-at-index-1-3/101466>\
**Category:** New to Julia\
**Created:** [July 11, 2023, 1:38am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-120-times-1-matrix-float-at-index-1-3/101466 "2023-07-11T01:38:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiaoting\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaoting_wang/32/49818_2.png) [@Xiaoting\_Wang](https://discourse.julialang.org/u/Xiaoting_Wang)\
**Post date:** [July 11, 2023, 1:38am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-120-times-1-matrix-float-at-index-1-3/101466/1 "2023-07-11T01:38:02Z")

</div>

Pout[:, 3:5] = X[:, 3:5]  
for i in 1:3  
PF\_coe = Param\_5[2 + i]  
for j in 1:120  
Qout[j, 2 + i] = Pout[j, 2 + i] \* PF\_coe  
end  
end  
Not understand why it keeps resulting in the boundserror, I did not find where it is wrong. Could anyone help me to solve this problem?

---

<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 11, 2023, 7:16am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-120-times-1-matrix-float-at-index-1-3/101466/2 "2023-07-11T07:16:37Z")

</div>

Julia is column major, the first index is the column index:

```julia
julia> a = [1; 2; 3;;]
3×1 Matrix{Int64}:
 1
 2
 3

julia> a[1,3]
ERROR: BoundsError: attempt to access 3×1 Matrix{Int64} at index [1, 3]
Stacktrace:
 [1] getindex(::Matrix{Int64}, ::Int64, ::Int64)
   @ Base ./essentials.jl:14
 [2] top-level scope
   @ REPL[3]:1

julia> a[3,1]
3

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 11, 2023, 7:23am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-120-times-1-matrix-float-at-index-1-3/101466/3 "2023-07-11T07:23:52Z")

</div>

I agree that the problem seems to be that

```julia
Qout[j, 2 + i] = Pout[j, 2 + i] * PF_coe 

```

should be

```julia
Qout[2 + i, j] = Pout[2 + i, j] * PF_coe

```

But this:

> [@Sukera](#):
>
> Julia is column major, the first index is the column index:

is unrelated. The first index is the _row_ index in both column major and row major languages.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [July 11, 2023, 7:48am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-120-times-1-matrix-float-at-index-1-3/101466/4 "2023-07-11T07:48:20Z")

</div>

I think it’s considerably more likely that `Qout` doesn’t have enough columns on construction and that this code is ported from Matlab where indexing out of bounds on the left hand side magically adds columns.

```julia
octave:1> q = zeros(120, 1);
octave:2> q(1, 3) = 1;
octave:3> size(q)
ans =

   120 3

```

```julia
julia> q = zeros(120, 1);

julia> q[1, 3] = 1
ERROR: BoundsError: attempt to access 120×1 Matrix{Float64} at index [1, 3]

```

---

<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 11, 2023, 8:21am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-120-times-1-matrix-float-at-index-1-3/101466/5 "2023-07-11T08:21:34Z")

</div>

> [@DNF](#):
>
> is unrelated. The first index is the _row_ index in both column major and row major languages.

It’s a difference in perspective - I take the first index to mean “which column should I look at?”, while you take it to mean “which element of the row should I look at?”. I.e., what I called “column index” is the “index of the column in the row”. From my POV, the second index gives me the row of a column, and the first index gives me the column in a table.

> [@GunnarFarneback](#):
>
> I think it’s considerably more likely that `Qout` doesn’t have enough columns on construction and that this code is ported from Matlab where indexing out of bounds on the left hand side magically adds columns.

That’s also a good point - perhaps @Xiaoting_Wang can clarify?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 11, 2023, 9:24am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-120-times-1-matrix-float-at-index-1-3/101466/6 "2023-07-11T09:24:21Z")

</div>

> [@Sukera](#):
>
> It’s a difference in perspective - I take the first index to mean “which column should I look at?”, while you take it to mean “which element of the row should I look at?”.

No, I take the first index to mean which _row_ to look at. And so do you, I suspect, you’re just mixing it up at the moment.

But my main point was that row major and column major languages have precisely the same meaning for the order of indices. The only difference is the memory layout, which is not visible to the user.

---

<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 11, 2023, 11:12am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-120-times-1-matrix-float-at-index-1-3/101466/7 "2023-07-11T11:12:03Z")

</div>

You’re right, in that message I was mixing things up - I was thinking of “which element in the column” (i.e., the index into the column), not “which column”!
