![]() matrix(c(1, rep(0, n)), nrow = n, ncol = n)īecause $n^2$ isn’t an exact multiple of $n+1$, R will throw a warning, which you can ignore. That is, R will repeat a vector to fit the space allocated if it isn’t already long enough. Guillaume’s own version doesn’t fit on one line but is a bit more sensible: x <- rep(0, n * n)Ĭolin Fay makes a more readable solution, taking advantage of matrix broadcasting. We can write down such a sequence and then reshape it. a one, then $n$ zeroes, another one, another $n$ zeroes and so on, finishing with the $n$th one. The three-dimensional identity matrix, for example, is It is a diagonal matrix of ones, with all off-diagonal entries equal to zero. The identity matrixĪs a quick reminder, the identity matrix is the linear algebraic equivalent of the number 1. Taking the scenic route and learning along the way.Ĭrazy code golf, if you like. ![]() This R challenge is less about minimising your golf handicap, however, and more about showing off some features and functions in R that others might not know about. In code golf, programmers try to write an algorithm using the shortest programme possible, often exploiting lesser-known eccentricies of each programming language. This was an interesting little challenge set by Guillaume Nicoulaud on Twitter. How many different ways are there to create an identity matrix in R? ![]() Tea & Stats Tea & Stats Data science with David Selby.
0 Comments
Leave a Reply. |