# Simple question about Array and Subarrays

**URL:** <https://discourse.julialang.org/t/simple-question-about-array-and-subarrays/64346>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 9, 2021, 12:15pm UTC](https://discourse.julialang.org/t/simple-question-about-array-and-subarrays/64346 "2021-07-09T12:15:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RCHG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rchg/32/10104_2.png) [@RCHG](https://discourse.julialang.org/u/RCHG)\
**Post date:** [July 9, 2021, 12:15pm UTC](https://discourse.julialang.org/t/simple-question-about-array-and-subarrays/64346/1 "2021-07-09T12:15:44Z")

</div>

Let’s say that for a given dimension d, we need to constrain the matrix to an specific value (or index). In other words, we need the subarray in which that dimension has an specific value (or index). How I can do something like:

B = A[where dim=d has index = i]

Maybe there is an specific method in Julia that I don’t know.

The motivation is that we read a variable A from a netcdf file, and the “position” of an specific coordinate in a A, in such way that depending on input netcdf that dimension (coordinate) can be in arbitrary axis in the array of the variable A. We need to extract a subarray based on the value of an specific coordinate whose position in the numerical array is variable depending on the input netcdf file.

Thank you.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 9, 2021, 12:54pm UTC](https://discourse.julialang.org/t/simple-question-about-array-and-subarrays/64346/2 "2021-07-09T12:54:04Z")

</div>

Could you provide a MWE?  
Also, just in case, there is a [NetCDF.jl](https://github.com/JuliaGeo/NetCDF.jl) package.

---

<div class="post-metadata">

**Author:** ![RCHG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rchg/32/10104_2.png) [@RCHG](https://discourse.julialang.org/u/RCHG)\
**Post date:** [July 9, 2021, 1:44pm UTC](https://discourse.julialang.org/t/simple-question-about-array-and-subarrays/64346/3 "2021-07-09T13:44:05Z")

</div>

Hello and thank you!

I am using NCDatasets.jl, and I could use GeoData.jl and actually I can do what I want. But I was wondering how to do without GeoData.jl

```julia
using NCDatasets
using GeoData

inforule = Dict("zlev" => 3)
diag = "o3"
finput = "o3_dataset.nc"

function geomean_atlev(diag, finput, inforule)

    ds_diag = Dataset(finput)[diag]
    data = geoarray(finput)
    subdata = Array( data[z=inforule["zlev"] ] )
    data = convert(Float64, mean(Array(subdata)))

    return diag, finput, data, inforule

end

```

For example how to get the subdarray named “subdata” above in the code like this below, where  
I know that the axis where I want to constrain the array is “posz”

```julia
using NCDatasets

inforule = Dict("zlev" => 3)
diag = "o3"
finput = "o3_dataset.nc"

function geomean_atlev(diag, finput, inforule)

    ds_diag = Dataset(finput)[diag]
    dimsizes= dimsize(ds_diag)
    posz=0
    let counter=0
    for (index, value) in collect(pairs(dimsizes))
        counter=counter+1
        if index==:z
           posz=counter
        end
    end
    [...]

    end # let

end #function
```

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [July 9, 2021, 3:37pm UTC](https://discourse.julialang.org/t/simple-question-about-array-and-subarrays/64346/4 "2021-07-09T15:37:14Z")

</div>

> [@RCHG](#):
>
> In other words, we need the subarray in which that dimension has an specific value (or index). How I can do something like:
> 
> B = A[where dim=d has index = i]

I don’t fully understand your problem, but this toy example using `selectdim`, `view`s, and `BitMatrix` might help. The `selectdim` function creates a `view`. Modifying a `view` modifies the original matrix. The `BitMatrix` type can be used for [logical indexing](https://docs.julialang.org/en/v1/manual/arrays/#Logical-indexing).

```julia
julia> dim = 2; index = 3; oldvalue = 7; newvalue = 10;

julia> A = [5 6 7; 7 4 7; 9 7 6]
3×3 Matrix{Int64}:
 5 6 7
 7 4 7
 9 7 6

julia> Aview = selectdim(A, dim, index)
3-element view(::Matrix{Int64}, :, 3) with eltype Int64:
 7
 7
 6

julia> B = falses(size(A))
3×3 BitMatrix:
 0 0 0
 0 0 0
 0 0 0

julia> Bview = selectdim(B, dim, index)
3-element view(::BitMatrix, :, 3) with eltype Bool:
 0
 0
 0

julia> Bview .= Aview.==oldvalue
3-element view(::BitMatrix, :, 3) with eltype Bool:
 1
 1
 0

julia> B
3×3 BitMatrix:
 0 0 1
 0 0 1
 0 0 0

julia> A[B] .= newvalue
2-element view(::Vector{Int64}, [7, 8]) with eltype Int64:
 10
 10

julia> A
3×3 Matrix{Int64}:
 5 6 10
 7 4 10
 9 7 6

```

---

<div class="post-metadata">

**Author:** ![RCHG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rchg/32/10104_2.png) [@RCHG](https://discourse.julialang.org/u/RCHG)\
**Post date:** [July 9, 2021, 3:53pm UTC](https://discourse.julialang.org/t/simple-question-about-array-and-subarrays/64346/5 "2021-07-09T15:53:16Z")

</div>

Thank you @Nathan_Boyer  
Yes, I think that selectdim is what I was looking for. If A is a 3D array, then

```julia
Aview = selectdim(A, dim, index)

```

would be the 2D matrix that I was looking for. 😀

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [July 9, 2021, 4:00pm UTC](https://discourse.julialang.org/t/simple-question-about-array-and-subarrays/64346/6 "2021-07-09T16:00:05Z")

</div>

If you do have trouble converting to 3D, don’t be afraid to just `for` loop through the third dimension. Loops are fast in Julia.
