Press "Enter" to skip to content

Returning Matrix Elements in Spiral Order in R

Tomaz Kastrun forgot to remove The Club from his REPL:

Another one from the Leetcode challenge. This time, get the elements (single values) from the matrix in a spiral order with a starting position of [1,1].

So, the basic idea is to retrieve a vector of elements from a matrix in the following order:

Probably not something you’d use with any frequency, but it’s a fun way to learn how to operate within matrices.