如何对每个矩阵元素的索引应用函数 [英] How to apply function over each matrix element's indices

查看:212
本文介绍了如何对每个矩阵元素的索引应用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道R中是否有一个内置函数,它将一个函数应用于矩阵的每个元素(当然,该函数应该基于矩阵索引计算)。相当于这样的东西:

  matrix_apply<  -  function(m,f){
m2 < - m
for(r in seq(nrow(m2)))
for(c in seq(ncol(m2)))
m2 [[r,c]] < (r,c)
return(m2)
}

如果有没有这样的内置函数,什么是初始化矩阵以包含通过计算具有矩阵索引作为参数的任意函数获得的值的最佳方法?

解决方案

我怀疑你想要 outer

  >垫<  - 矩阵(NA,nrow = 5,ncol = 3)

>外部(1:nrow(mat),1:ncol(mat),FUN =*)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 4 6
[3,] 3 6 9
[4,] 4 8 12
[5,] 5 10 15

>外部(1:nrow(mat),1:ncol(mat),FUN = function(r,c)log(r + c))
[,1] [,2] [,3]
[1,] 0.6931472 1.098612 1.386294
[2,] 1.0986123 1.386294 1.609438
[3,] 1.3862944 1.609438 1.791759
[4,] 1.6094379 1.791759 1.945910
[5,] 1.7917595 1.945910 2.079442

这将产生一个很好的紧凑输出。但是有可能 mapply 在其他情况下会很有用。想要 mapply 是另一种方式来执行与此页面上的其他人使用相同的操作, Vectorize 对于。 mapply 由于无法 Vectorize 使用原始功能,因此更为通用。

  data.frame(mrow = c(row(mat)),#拉直参数
mcol = c(col(mat)),
mfres = mapply(function(r,c)log(r + c),row(mat),col(mat)))
#mrow mcol mfres
1 1 1 0.6931472
2 2 1 1.0986123
3 3 1 1.3862944
4 4 1 1.6094379
5 5 1 1.7917595
6 1 2 1.0986123
7 2 2 1.3862944
8 3 2 1.6094379
9 4 2 1.7917595
10 5 2 1.9459101
11 1 3 1.3862944
12 2 3 1.6094379
13 3 3 1.7917595
14 4 3 1.9459101
15 5 3 2.0794415

你可能不是真的提供该函数的行()和col()函数将返回:这产生一个15(有点冗余)3 x 5矩阵的数组:

 >外部(行(mat),col(mat),FUN = function(r,c)log(r + c))


I am wondering if there is a built-in function in R which applies a function to each element of the matrix (of course, the function should be computed based on matrix indices). The equivalent would be something like this:

matrix_apply <- function(m, f) {
  m2 <- m
  for (r in seq(nrow(m2)))
    for (c in seq(ncol(m2)))
      m2[[r, c]] <- f(r, c)
  return(m2)
}

If there is no such built-in function, what is the best way to initialize a matrix to contain values obtained by computing an arbitrary function which has matrix indices as parameters?

解决方案

I suspect you want outer:

> mat <- matrix(NA, nrow=5, ncol=3)

> outer(1:nrow(mat), 1:ncol(mat) , FUN="*")
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    6
[3,]    3    6    9
[4,]    4    8   12
[5,]    5   10   15

> outer(1:nrow(mat), 1:ncol(mat) , FUN=function(r,c) log(r+c) )
          [,1]     [,2]     [,3]
[1,] 0.6931472 1.098612 1.386294
[2,] 1.0986123 1.386294 1.609438
[3,] 1.3862944 1.609438 1.791759
[4,] 1.6094379 1.791759 1.945910
[5,] 1.7917595 1.945910 2.079442

That yields a nice compact output. but it's possible that mapply would be useful in other situations. It is helpful to think of mapply as just another way to do the same operation that others on this page are using Vectorize for. mapply is more general because of the inability Vectorize to use "primitive" functions.

data.frame(mrow=c(row(mat)),   # straightens out the arguments
           mcol=c(col(mat)), 
           m.f.res= mapply(function(r,c) log(r+c), row(mat), col(mat)  ) )
#   mrow mcol   m.f.res
1     1    1 0.6931472
2     2    1 1.0986123
3     3    1 1.3862944
4     4    1 1.6094379
5     5    1 1.7917595
6     1    2 1.0986123
7     2    2 1.3862944
8     3    2 1.6094379
9     4    2 1.7917595
10    5    2 1.9459101
11    1    3 1.3862944
12    2    3 1.6094379
13    3    3 1.7917595
14    4    3 1.9459101
15    5    3 2.0794415

You probably didn't really mean to supply to the function what the row() and col() functions would have returned: This produces an array of 15 (somewhat redundant) 3 x 5 matrices:

> outer(row(mat), col(mat) , FUN=function(r,c) log(r+c) )

这篇关于如何对每个矩阵元素的索引应用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆