Description Usage Arguments Value Author(s) Examples

The function `fpos`

returns the locations (row and column index) where a small matrix may be found in a larger matrix. The function also works with vectors.

1 |

`needle` |
A matrix or vector to search for in the larger matrix or vector |

`haystack` |
A matrix or vector to look into. |

`all` |
A logical value to indicate whether to return all occurences ( |

`overlap` |
A logical value to indicate whether to allow the small matrix occurences to overlap or not. Default value is |

A two columns matrix that contains the position or index where the small matrix (needle) can be found in the larger matrix. The first column refers to rows and the second to columns. In case both the needle and haystack are vectors, a vector is returned.

Morgan Jacob

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | ```
# Example 1: find a matrix inside a larger one
big_matrix = matrix(c(1:30), nrow = 10)
small_matrix = matrix(c(14, 15, 24, 25), nrow = 2)
fpos(small_matrix, big_matrix)
# Example 2: find a vector inside a larger one
fpos(14:15, 1:30)
# Example 3:
big_matrix = matrix(c(1:5), nrow = 10, ncol = 5)
small_matrix = matrix(c(2:3), nrow = 2, ncol = 2)
# return all occurences
fpos(small_matrix, big_matrix)
# return only the first
fpos(small_matrix, big_matrix, all = FALSE)
# return non overlapping occurences
fpos(small_matrix, big_matrix, overlap = FALSE)
# Benchmarks
# ----------
# x = matrix(1:5, nrow=1e4, ncol=5e3) # 191Mb
# microbenchmark::microbenchmark(
# fpos=kit::fpos(1L, x),
# which=which(x==1L, arr.ind=TRUE),
# times=10L
# )
# Unit: milliseconds
# expr min lq mean median uq max neval
# fpos 202 206 220 221 231 241 10
# which 612 637 667 653 705 724 10
``` |

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.