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


#1

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


#2

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.

#3

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

#4


if you want a view


#5

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


#6

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.