Is there a way to get 'row' SubArrays?


#1

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


#2

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.


#3

thanks,

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


#4

```julia
… your code here
```