# Is there a way to get 'row' SubArrays?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-get-row-subarrays/3102>\
**Category:** New to Julia\
**Created:** [April 7, 2017, 9:33am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-row-subarrays/3102 "2017-04-07T09:33:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![uliano](https://avatars.discourse-cdn.com/v4/letter/u/e47c2d/32.png) [@uliano](https://discourse.julialang.org/u/uliano)\
**Post date:** [April 7, 2017, 9:33am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-row-subarrays/3102/1 "2017-04-07T09:33:59Z")

</div>

To my understanding if I dont want to copy a part of and array a use view, like this:

julia\> a=rand(4,4)  
4×4 Array{Float64,2}:  
0.256046 0.0541067 0.0618461 0.657153  
0.968974 0.652169 0.783263 0.323621  
0.543603 0.529372 0.698797 0.255175  
0.0671429 0.470615 0.148772 0.0287616

julia\> b=view(a,:,2)  
4-element SubArray{Float64,1,Array{Float64,2},Tuple{Colon,Int64},true}:  
0.0541067  
0.652169  
0.529372  
0.470615

extracting a column has collapsed the dimension of the array from 2 to 1 and I get a monodimensional vector, which is nice as vectors behave as columns. But if i try to extract a row:

julia\> c=view(a,2,: )  
4-element SubArray{Float64,1,Array{Float64,2},Tuple{Int64,Colon},true}:  
0.968974  
0.652169  
0.783263  
0.323621

I still get a monodimensional ‘column’ vector , yes I can transpose:

julia\> c=view(a,2,:)’  
1×4 Array{Float64,2}:  
0.968974 0.652169 0.783263 0.323621

but this way I get a copy loosing the purpose of getting a view

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [April 7, 2017, 10:35am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-row-subarrays/3102/2 "2017-04-07T10:35:50Z")

</div>

If you want to avoid the collapsing of dimensions, use a length-1 range: `view(a, 2:2,:)`  
BTW your post becomes more readable if you put the code in code blocks.

---

<div class="post-metadata">

**Author:** ![uliano](https://avatars.discourse-cdn.com/v4/letter/u/e47c2d/32.png) [@uliano](https://discourse.julialang.org/u/uliano)\
**Post date:** [April 7, 2017, 10:51am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-row-subarrays/3102/3 "2017-04-07T10:51:49Z")

</div>

thanks,

… I was just wondering how to do it  
(code blocks)

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [April 7, 2017, 10:57am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-row-subarrays/3102/4 "2017-04-07T10:57:17Z")

</div>

```julia  
… your code here  
```
