[R] Pixel Image Reshaping using R

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Thu Feb 24 18:00:13 CET 2022


On Thu, 24 Feb 2022 11:00:08 -0500
Paul Bernal <paulbernal07 using gmail.com> wrote:

> Each pixel column in the training set has a name like pixel x, where
> x is an integer between 0 and 783, inclusive. To locate this pixel on
> the image, suppose that we have decomposed x as x = i ∗ 28 + j, where
> i and j are integers between 0 and 27, inclusive. 

> I have been looking for information about how to process this with R,
> but have not found anything yet.

Given a 784-element vector x, you can reshape it into a 28 by 28 matrix:

dim(x) <- c(28, 28)

Or create a new matrix: matrix(x, 28, 28)

Working with more dimensions is also possible. A matrix X with dim(X)
== c(n, 784) can be transformed into a three-way array in place or
copied into one:

dim(X) <- c(dim(X)[1], 28, 28)
array(X, c(dim(X)[1], 28, 28))

(Replace 28,28 with 784 for an inverse transformation. In modern
versions of R, two-way arrays are more or less the same as matrices,
but old versions may disagree with that in some corner cases.)

For more information, see ?dim, ?matrix, ?array.

-- 
Best regards,
Ivan



More information about the R-help mailing list