# Is there any padding function available to pad a matrix or array?

**URL:** <https://discourse.julialang.org/t/is-there-any-padding-function-available-to-pad-a-matrix-or-array/8521>\
**Category:** General Usage\
**Tags:** arrays\
**Created:** [January 22, 2018, 11:44am UTC](https://discourse.julialang.org/t/is-there-any-padding-function-available-to-pad-a-matrix-or-array/8521 "2018-01-22T11:44:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tosalonijain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tosalonijain/32/3624_2.png) [@tosalonijain](https://discourse.julialang.org/u/tosalonijain)\
**Post date:** [January 22, 2018, 11:44am UTC](https://discourse.julialang.org/t/is-there-any-padding-function-available-to-pad-a-matrix-or-array/8521/1 "2018-01-22T11:44:22Z")

</div>

If yes, Please share, if not than also a response would be appreciated.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 22, 2018, 11:53am UTC](https://discourse.julialang.org/t/is-there-any-padding-function-available-to-pad-a-matrix-or-array/8521/2 "2018-01-22T11:53:35Z")

</div>

Please clarify with a minimal working example:

1. presumably you want to pad with `0`s, but how? rows, columns, above, below, …
2. what would you do with the result? you may be much better off with sparse matrices.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 22, 2018, 12:14pm UTC](https://discourse.julialang.org/t/is-there-any-padding-function-available-to-pad-a-matrix-or-array/8521/3 "2018-01-22T12:14:01Z")

</div>

The `padarray` function in the `Images` package might be what you are looking for.  
E.g.

```julia
julia> using Images

julia> x = [1 2 3;4 5 6]
2×3 Array{Int64,2}:
 1 2 3
 4 5 6

julia> y = padarray(x, Pad(1, 1))
OffsetArrays.OffsetArray{Int64,2,Array{Int64,2}} with indices 0:3×0:4:
 1 1 2 3 3
 1 1 2 3 3
 4 4 5 6 6
 4 4 5 6 6

```

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [January 22, 2018, 12:27pm UTC](https://discourse.julialang.org/t/is-there-any-padding-function-available-to-pad-a-matrix-or-array/8521/4 "2018-01-22T12:27:02Z")

</div>

[https://github.com/JuliaArrays/PaddedViews.jl](https://github.com/JuliaArrays/PaddedViews.jl)  
if you want a view

---

<div class="post-metadata">

**Author:** ![tosalonijain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tosalonijain/32/3624_2.png) [@tosalonijain](https://discourse.julialang.org/u/tosalonijain)\
**Post date:** [January 22, 2018, 3:21pm UTC](https://discourse.julialang.org/t/is-there-any-padding-function-available-to-pad-a-matrix-or-array/8521/5 "2018-01-22T15:21:45Z")

</div>

Where exactly is this padarray in the images package? I could not find it.

---

<div class="post-metadata">

**Author:** ![Oliver\_Lylloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver_lylloff/32/9022_2.png) [@Oliver\_Lylloff](https://discourse.julialang.org/u/Oliver_Lylloff)\
**Post date:** [January 22, 2018, 3:36pm UTC](https://discourse.julialang.org/t/is-there-any-padding-function-available-to-pad-a-matrix-or-array/8521/6 "2018-01-22T15:36:11Z")

</div>

ImageFiltering.jl has one for sure…

EDIT: For reference, here’s the docs for padarray: [https://juliaimages.github.io/latest/function\_reference.html#ImageFiltering.padarray](https://juliaimages.github.io/latest/function_reference.html#ImageFiltering.padarray)

It lives in ImageFiltering.jl but is exported when `using Images` as @GunnarFarneback pointed out above.

---

<div class="post-metadata">

**Author:** ![Arghavan\_Partovifar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arghavan_partovifar/32/18262_2.png) [@Arghavan\_Partovifar](https://discourse.julialang.org/u/Arghavan_Partovifar)\
**Post date:** [August 11, 2021, 2:43pm UTC](https://discourse.julialang.org/t/is-there-any-padding-function-available-to-pad-a-matrix-or-array/8521/7 "2021-08-11T14:43:48Z")

</div>

You can use **resample** function in FourierTools package.  
[https://bionanoimaging.github.io/FourierTools.jl/dev/resampling/](https://bionanoimaging.github.io/FourierTools.jl/dev/resampling/)

---

<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:** [August 11, 2021, 4:54pm UTC](https://discourse.julialang.org/t/is-there-any-padding-function-available-to-pad-a-matrix-or-array/8521/8 "2021-08-11T16:54:53Z")

</div>

The advice from @Gnimuc to use PaddedViews.jl seems pretty good:

```julia
using PaddedViews
a = rand((sqrt(2),im), 3, 3)
b = PaddedView(π, a, (4, 5))

  1.41421 im im π π
 im im 1.41421 π π
  1.41421 1.41421 im π π
  π π π π π

```
