# For loop in 2D array

**URL:** <https://discourse.julialang.org/t/for-loop-in-2d-array/45175>\
**Category:** General Usage\
**Tags:** loops\
**Created:** [August 18, 2020, 6:38pm UTC](https://discourse.julialang.org/t/for-loop-in-2d-array/45175 "2020-08-18T18:38:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Renan\_Rabelo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/renan_rabelo/32/8048_2.png) [@Renan\_Rabelo](https://discourse.julialang.org/u/Renan_Rabelo)\
**Post date:** [August 18, 2020, 6:38pm UTC](https://discourse.julialang.org/t/for-loop-in-2d-array/45175/1 "2020-08-18T18:38:23Z")

</div>

How do I use a for loop to iterate in a 2D array only in a specific row or column?

# pseudo code

a = [1 2; 3 4]

for x in a only row[1]  
println(x)  
end

# output

1  
2

Thanks!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 18, 2020, 6:43pm UTC](https://discourse.julialang.org/t/for-loop-in-2d-array/45175/2 "2020-08-18T18:43:46Z")

</div>

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

julia> for i in a[:, 1]
       println(i)
       end
1
3

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 18, 2020, 7:05pm UTC](https://discourse.julialang.org/t/for-loop-in-2d-array/45175/3 "2020-08-18T19:05:02Z")

</div>

> [@pdeffebach](#):
>
> `a[:, 1]`

Note that `a[:, 1]` makes a _copy_ of that data, which is unnecessary in this case. `@view a[:, 1]` avoids the copy and should be faster if `a` is a large matrix.
