Resample a vector of Matrices

When performing a simulation of a complex system, one often uses a small time step, resulting in large resulting solution arrays. Given Julia’s excellent speed, creating these arrays is often not terribly time consuming, but one does run into trouble when attempting to plot arrays with tens of millions of points.

A nice solution is to simply resample the solution arrays and plot the resulting arrays.

Suppose you have a Vector of Matrices. For a concrete example: suppose you have a three element vector, each element being an Nx2 matrix. Then, we can define a function resample_matrix which starts at the first row and samples every nth row until the end of the matrix:

resample_matrix(m::Matrix{Float64}, n::Int) = m[1:n:end, : ]   # resample every nth row

Now we can take an input vector (which I’ll call V) and create an array V_resampled where I sample every tenth point:

V_resampled = map(m->resample_matrix(m,10), V)

The map function takes the first element of V (an Nx2 matrix) and feeds that into the resample_matrix function where n=10. Then, the map function continues with this process for each element of V.

Yes, I know I haven’t made the resample_matrix function user-friendly by including any error checking etc. The point here is to just give a quick code example that will work.

About PaulNakroshis

Professor of Physics at the University of Southern Maine.
This entry was posted in Array, Julia, resampling and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.