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

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

1 Like

Please clarify with a minimal working example:

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

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

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

https://github.com/JuliaArrays/PaddedViews.jl
if you want a view

2 Likes

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

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

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

You can use resample function in FourierTools package.
https://bionanoimaging.github.io/FourierTools.jl/dev/resampling/

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

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        π  π
  π         π         π        π  π
1 Like