Skip to main content
added 525 characters in body
Source Link
Dominic van Essen
  • 36.4k
  • 2
  • 22
  • 60

R, 19 bytes

\(m,x)which(m==x,T)

Attempt This Online!

R has almost exactly the correct built-in for this.


R, 6464 59 bytes

Edit: -5 bytes thanks to pajonk

\(m,x,k=as.matrixk=t(t(expand.grid(lapply(dim(m),`:`,1)))))k[m[k]==x,]

Attempt This Online!Attempt This Online!

Roll-your-own solution without using which.

expand.grid is used to generate all possible sets of indices, which are then tested to see whether the element is equal to the searched-for x. Unfortunately, expand.grid outputs a "data.frame" (an R-specific matrix-like data type, but that isn't a matrix): pajonk's golf is to transpose twice (t(t(...))) to convert into a matrix, saving 5 bytes compared to the more-conventional as.matrix(...)...

R, 19 bytes

\(m,x)which(m==x,T)

Attempt This Online!

R has almost exactly the correct built-in for this.


R, 64 bytes

\(m,x,k=as.matrix(expand.grid(lapply(dim(m),`:`,1))))k[m[k]==x,]

Attempt This Online!

Roll-your-own solution without using which.

R, 19 bytes

\(m,x)which(m==x,T)

Attempt This Online!

R has almost exactly the correct built-in for this.


R, 64 59 bytes

Edit: -5 bytes thanks to pajonk

\(m,x,k=t(t(expand.grid(lapply(dim(m),`:`,1)))))k[m[k]==x,]

Attempt This Online!

Roll-your-own solution without using which.

expand.grid is used to generate all possible sets of indices, which are then tested to see whether the element is equal to the searched-for x. Unfortunately, expand.grid outputs a "data.frame" (an R-specific matrix-like data type, but that isn't a matrix): pajonk's golf is to transpose twice (t(t(...))) to convert into a matrix, saving 5 bytes compared to the more-conventional as.matrix(...)...

added 499 characters in body
Source Link
Dominic van Essen
  • 36.4k
  • 2
  • 22
  • 60

R, 19 bytes

\(m,x)which(m==x,T)

Attempt This Online!

R has almost exactly the correct built-in for this.


R, 64 bytes

\(m,x,k=as.matrix(expand.grid(lapply(dim(m),`:`,1))))k[m[k]==x,]

Attempt This Online!

Roll-your-own solution without using which.

R, 19 bytes

\(m,x)which(m==x,T)

Attempt This Online!

R has almost exactly the correct built-in for this.

R, 19 bytes

\(m,x)which(m==x,T)

Attempt This Online!

R has almost exactly the correct built-in for this.


R, 64 bytes

\(m,x,k=as.matrix(expand.grid(lapply(dim(m),`:`,1))))k[m[k]==x,]

Attempt This Online!

Roll-your-own solution without using which.

Source Link
Dominic van Essen
  • 36.4k
  • 2
  • 22
  • 60

R, 19 bytes

\(m,x)which(m==x,T)

Attempt This Online!

R has almost exactly the correct built-in for this.