# Mat2grid question

**URL:** <https://discourse.julialang.org/t/mat2grid-question/70645>\
**Category:** General Usage\
**Tags:** question, gmt\
**Created:** [October 30, 2021, 12:01am UTC](https://discourse.julialang.org/t/mat2grid-question/70645 "2021-10-30T00:01:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 30, 2021, 12:01am UTC](https://discourse.julialang.org/t/mat2grid-question/70645/1 "2021-10-30T00:01:35Z")

</div>

Sorry for the very basic question. Unfortunately, I’m struggling to get it to work. Is below the right way to use `mat2grid`? My variable `A` stores the temperature values at a grid that is defined by `lon` and `lat` with the size of 1440 x 720.

```julia
lon = collect(20:0.25:379.75);
lat = collect(-90:0.25:89.75);
B = mat2grid(A, x=lon, y=lat);

```

Here are their sizes:

```julia
size(A) = (1440, 720)
size(lon) = (1440,)
size(lat) = (720,)

```

What did I do wrong? Many thanks!

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [October 30, 2021, 12:08am UTC](https://discourse.julialang.org/t/mat2grid-question/70645/2 "2021-10-30T00:08:34Z")

</div>

`lon` is **x** and `lat` is **y** but your `A` grid has _lon size_ rows and _lat size_ columns. Is your `A` matrix transposed?

Note, maybe ask this type of questions in the GMT forum?

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 30, 2021, 12:18am UTC](https://discourse.julialang.org/t/mat2grid-question/70645/3 "2021-10-30T00:18:23Z")

</div>

Many thanks. Unfortunately, now I have a new error as below.

So this is the official GMT forum? Glad to hear about that. I’ll try to ask this type of questions over there:

> **[GMT Community Forum](https://forum.generic-mapping-tools.org/)**
>
> A forum for discussions and Q&A about the Generic Mapping Tools

Error message:

```julia
**ERROR:** LoadError: MethodError: no method matching GMTgrid(::String, ::String, ::Int64, ::Vector{Float64}, ::Vector{Float64}, ::Int64, ::Float64, ::String, ::String, ::String, ::Vector{String}, ::Vector{Float64}, ::Vector{Float64}, ::Vector{Float64}, ::LinearAlgebra.Adjoint{Float64, Matrix{Float64}}, ::String, ::String, ::String, ::String, ::String, ::Float32, ::Float32, ::Int64)

Closest candidates are:
GMTgrid(::String, ::String, ::Int64, ::Vector{Float64}, ::Vector{Float64}, ::Int64, ::Union{Float32, Float64}, ::String, ::String, ::String, ::Vector{String}, ::Vector{Float64}, ::Vector{Float64}, ::Union{Vector{String}, Vector{<:Real}}, ::Array{T, N}, ::String, ::String, ::String, ::String, ::String, ::Union{Float32, Float64}, ::Union{Float32, Float64}, ::Int64) where {T<:Real, N} at ~/.julia/packages/GMT/TPV4a/src/gmt_main.jl:2
Stacktrace:
[1] **mat2grid(** mat::LinearAlgebra.Adjoint{Float64, Matrix{Float64}}, xx::Vector{Float64}, yy::Vector{Float64}; reg::Nothing, x::Vector{Float64}, y::Vector{Float64}, v::Vector{Float64}, hdr::Nothing, proj4::String, wkt::String, epsg::Int64, tit::String, rem::String, cmd::String, names::Vector{String}, scale::Float32, offset::Float32 **)**
@ GMT ~/.julia/packages/GMT/TPV4a/src/utils_types.jl:622

```

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [October 30, 2021, 12:22am UTC](https://discourse.julialang.org/t/mat2grid-question/70645/4 "2021-10-30T00:22:43Z")

</div>

> [@leon](#):
>
> So this is the official GMT forum?

Yes, but I live on GMT+1, the _timezone_ (and very soon GMT+0), probably will look only tomorrow. Please provide all info that I need to reproduce the case.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 30, 2021, 12:26am UTC](https://discourse.julialang.org/t/mat2grid-question/70645/5 "2021-10-30T00:26:41Z")

</div>

Yes, will do. Many thanks for all of your help! 👍 Good night 💤

---

<div class="post-metadata">

**Author:** ![MJulia](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@MJulia](https://discourse.julialang.org/u/MJulia)\
**Post date:** [October 30, 2021, 4:06am UTC](https://discourse.julialang.org/t/mat2grid-question/70645/6 "2021-10-30T04:06:57Z")

</div>

Use Array(A’) after A matrix transposed.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 30, 2021, 6:55am UTC](https://discourse.julialang.org/t/mat2grid-question/70645/7 "2021-10-30T06:55:45Z")

</div>

Just an aside, you will very rarely need to `collect` a range. Since a range is only 3 numbers (start, step, end) it’s way more storage efficient than a vector. Especially since vectors are mutable and therefore difficult to optimize around.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 30, 2021, 9:51am UTC](https://discourse.julialang.org/t/mat2grid-question/70645/8 "2021-10-30T09:51:19Z")

</div>

Many thanks for the solution! It works well 👍

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 30, 2021, 9:55am UTC](https://discourse.julialang.org/t/mat2grid-question/70645/9 "2021-10-30T09:55:14Z")

</div>

Gustaphe,

Thank you for that tip! So I should just do the below?  
`lon = [20:0.25:279.75];`

BTW, I find it weird that mat2grid is asking for the longitude and latitude vectors or ranges, instead of the gridded longitude and latitude. Won’t the latter be faster? How could I do that?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 30, 2021, 10:00am UTC](https://discourse.julialang.org/t/mat2grid-question/70645/10 "2021-10-30T10:00:16Z")

</div>

I have no idea about `mat2grid` - it gives “Binding `mat2grid` does not exist” on my machine, so it’s probably from some package you forgot to mention.

But unless it’s really poorly written, it should accept a range just as well as an array:

```julia
lon = 20:0.25:279.75

```

if you but that in square brackets, you are making a vector with one element of type `StepRangeLen`

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 30, 2021, 10:14am UTC](https://discourse.julialang.org/t/mat2grid-question/70645/11 "2021-10-30T10:14:34Z")

</div>

Thank you!

So you have an alternative function other than mat2grid?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 30, 2021, 10:33am UTC](https://discourse.julialang.org/t/mat2grid-question/70645/12 "2021-10-30T10:33:51Z")

</div>

Like I said, I don’t know what it does, so no.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [October 30, 2021, 3:05pm UTC](https://discourse.julialang.org/t/mat2grid-question/70645/13 "2021-10-30T15:05:20Z")

</div>

> [@leon](#):
>
> So you have an alternative function other than mat2grid?

Yes, you can create a [GMTgrid](https://www.generic-mapping-tools.org/GMT.jl/dev/types/#Grid-type) type yourself and making sure all mandatory fields are filled correctly.

`grids` are not just matrices. If you care about coordinates, you need to provide the _x,y_ origins, grid spacing. Coordinate system when that matters. Vertical coordinates when dealing with cubes, other metadata, etc. `mat2grid` is a convenient function to do all that, but users are ofc free to create the GMTgrid type by themselves.

> [@gustaphe](#):
>
> But unless it’s really poorly written, it should accept a range just as well as an array:

When data is to be transmitted to C you need to provide the actual numbers. C doesn’t know what are `UnitRange` or `LinearAlgebra.Adjoint` types.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 30, 2021, 3:38pm UTC](https://discourse.julialang.org/t/mat2grid-question/70645/14 "2021-10-30T15:38:57Z")

</div>

> [@joa-quim](#):
>
> When data is to be transmitted to C you need to provide the actual numbers. C doesn’t know what are `UnitRange` or `LinearAlgebra.Adjoint` types.

Yes. But that decision should be delayed as close as possible to the `ccall` imo. If there is a documented function `mat2grid`, I as a user shouldn’t need to know it’s going through C.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [October 30, 2021, 3:59pm UTC](https://discourse.julialang.org/t/mat2grid-question/70645/15 "2021-10-30T15:59:21Z")

</div>

```julia
G = mat2grid(rand(Float32, 129, 129), x=0:0.5:64, y=0:0.5:64);
 imshow(G, coast=true, cmap=:topo, proj=:guess)

```

A different story is to accept abstract arrays in the type and pay the performance penalty or complicate a lot the multiple dispatch of this function.

 ![GMTjl_tmp-fs8](https://global.discourse-cdn.com/julialang/original/3X/3/3/33782e0586146ec5ba7c85ca0f1f82b9929ba5b7.png)
