# Julia version of padarray in matlab

**URL:** <https://discourse.julialang.org/t/julia-version-of-padarray-in-matlab/37635>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 15, 2020, 11:27am UTC](https://discourse.julialang.org/t/julia-version-of-padarray-in-matlab/37635 "2020-04-15T11:27:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)\
**Post date:** [April 15, 2020, 11:27am UTC](https://discourse.julialang.org/t/julia-version-of-padarray-in-matlab/37635/1 "2020-04-15T11:27:58Z")

</div>

Hello,

I’ve been searching the documentations for some kind of julia alternative to the padarray function in matlab ([Pad array - MATLAB padarray](https://www.mathworks.com/help/images/ref/padarray.html)) but I did not find anything useful.

TL;DR: what i want to do is given an array `u=[1 2; 3 4]` pad it with the values on the border of my original array `u` (corresponding to the keywords `'replicate'` and `'both'` in the matlab couterpart)

```julia
padarray(u, [1, 1]) = [1 1 2 2
                       1 1 2 2
                       3 3 4 4
                       3 3 4 4]

```

does a function like this exists in julia or do i have to write my own?  
In the latter case, any smart way to do it?

Thanks

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [April 15, 2020, 11:49am UTC](https://discourse.julialang.org/t/julia-version-of-padarray-in-matlab/37635/2 "2020-04-15T11:49:31Z")

</div>

```julia
repeat(u, inner=[2,2])
?repeat

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [April 15, 2020, 11:51am UTC](https://discourse.julialang.org/t/julia-version-of-padarray-in-matlab/37635/3 "2020-04-15T11:51:11Z")

</div>

One way is from [here](https://github.com/JuliaImages/ImageFiltering.jl/pull/99/files):

```julia
julia> using Pkg; pkg"add ImageFiltering"

julia> using ImageFiltering

julia> arr = reshape(1:6, (2,3))
2×3 reshape(::UnitRange{Int64}, 2, 3) with eltype Int64:
 1 3 5
 2 4 6

julia> BorderArray(arr, Pad((1,1)))
4×5 BorderArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}},Pad{2}} with indices 0:3×0:4:
 1 1 3 5 5
 1 1 3 5 5
 2 2 4 6 6
 2 2 4 6 6

```

Note that this won’t in general agree with the result of repeat, e.g.:

```julia
julia> repeat(arr, inner=[2,2])
4×6 Array{Int64,2}:
 1 1 3 3 5 5
 1 1 3 3 5 5
 2 2 4 4 6 6
 2 2 4 4 6 6

```

Note also that for padding with a constant, but not with the edge values, there is [GitHub - JuliaArrays/PaddedViews.jl: Add virtual padding to the edges of an array](https://github.com/JuliaArrays/PaddedViews.jl)

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [April 15, 2020, 11:52am UTC](https://discourse.julialang.org/t/julia-version-of-padarray-in-matlab/37635/4 "2020-04-15T11:52:47Z")

</div>

Any reason to link to the _Pull Request_ instead of documentation?  
Think of the average user, he has no idea what he sees there.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [April 15, 2020, 11:53am UTC](https://discourse.julialang.org/t/julia-version-of-padarray-in-matlab/37635/5 "2020-04-15T11:53:46Z")

</div>

I looked but couldn’t find it in that package’s docs in a few minutes. But if you can, please add a link.

---

<div class="post-metadata">

**Author:** ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)\
**Post date:** [April 15, 2020, 11:58am UTC](https://discourse.julialang.org/t/julia-version-of-padarray-in-matlab/37635/6 "2020-04-15T11:58:17Z")

</div>

Thanks, i think this is what i am looking for, i’ll look into it!

> I looked but couldn’t find it in that package’s docs in a few minutes. But if you can, please add a link.

Sure!

Edit: Found this: [https://juliaimages.org/ImageFiltering.jl/stable/function\_reference.html#Boundaries-and-padding-1](https://juliaimages.org/ImageFiltering.jl/stable/function_reference.html#Boundaries-and-padding-1)

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [April 15, 2020, 12:03pm UTC](https://discourse.julialang.org/t/julia-version-of-padarray-in-matlab/37635/7 "2020-04-15T12:03:16Z")

</div>

Oh right, I missed that. The difference is that `padarray(arr, Pad((1,1)))` creates a new matrix with the padding, vs. `BorderArray` which is a view of the old one. One of these may be preferred depending on what you do next.

BTW both of them have unconventional indices, `axes(A,1) == 0:3` in the above examples, rather than `1:4`. Which means that the original pixels are at their original locations.

---

<div class="post-metadata">

**Author:** ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)\
**Post date:** [April 15, 2020, 12:08pm UTC](https://discourse.julialang.org/t/julia-version-of-padarray-in-matlab/37635/8 "2020-04-15T12:08:14Z")

</div>

Great, thanks for pointing that out! That is quite important information.

I must say, the docs of that package are quite lacking.  
I’ll probably make a PR to add some of these informations there.

---

<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:** [April 15, 2020, 12:10pm UTC](https://discourse.julialang.org/t/julia-version-of-padarray-in-matlab/37635/9 "2020-04-15T12:10:35Z")

</div>

> [@cshen](#):
>
> does a function like this exists in julia or do i have to write my own?  
> In the latter case, any smart way to do it?

To complement the links to existing solutions, the basic do it yourself implementation would be along these lines:

```julia
function padarray(a, p)
    h, w = size(a)
    y = clamp.((1 - p[1]):(h + p[1]), 1, h)
    x = clamp.((1 - p[2]):(w + p[2]), 1, w)
    return a[y, x]
end

```

(`BorderArray` is a lot smarter in terms of memory use, though.)

---

<div class="post-metadata">

**Author:** ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)\
**Post date:** [April 15, 2020, 12:32pm UTC](https://discourse.julialang.org/t/julia-version-of-padarray-in-matlab/37635/10 "2020-04-15T12:32:05Z")

</div>

> [@mcabbott](#):
>
> Oh right, I missed that. The difference is that `padarray(arr, Pad((1,1)))` creates a new matrix with the padding, vs. `BorderArray` which is a view of the old one. One of these may be preferred depending on what you do next.
> 
> BTW both of them have unconventional indices, `axes(A,1) == 0:3` in the above examples, rather than `1:4` . Which means that the original pixels are at their original locations.

Just to build a bit on this, these are some quick test i made that may be useful depending on the situation on which one would want to use these functions

```julia
H = 1000
pad = 2
u = rand(H, H)
@btime padarray($u, Pad((pad, pad))) # 72.718 μs (21 allocations: 7.71 MiB)
@btime BorderArray($u, Pad((pad, pad))) # 193.989 ns (3 allocations: 112 bytes)

function test_access(u)
    for i in eachindex(u)
        u[i]
    end
end

u_pad = padarray(u, Pad((pad, pad)))
u_border = BorderArray(u, Pad((pad, pad)))

@btime test_access($u_pad); # 288.715 μs (0 allocations: 0 bytes)
@btime test_access($u_border); # 3.544 ms (0 allocations: 0 bytes)

```
