数组 - R 中的向量乘法 [英] Array - vector multiplication in R

查看:61
本文介绍了数组 - R 中的向量乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个简单的矩阵代数或 Kronicker 乘积类型运算来将数组和 R 中的向量相乘以获得特定结果.假设我有数组:

I need a simple matrix-algebra or Kronicker product type operation to multiply an array and a vector in R to get to a specific result. Let's say I have the array:

ar<-array(c(rep(1,9),rep(10,9),rep(100,9)),dim=c(3,3,3))

还有向量c(1,2,3).将两者乘以 * 将数组的每张幻灯片上的每一行分别乘以 1,2 和 3.但是,我需要一个操作才能访问数组

And the vector c(1,2,3). Multiplying both by * multiplies each row on each slide of the array by 1,2, and 3 respectively. However, I need an operation to get to array

ar2<-array(c(rep(1,9),rep(20,9),rep(300,9)),dim=c(3,3,3))

相反.也就是说,是否有一个简单的操作可以让我使用上面指定的向量将 ar 转换为 ar2 ?谢谢!

instead. That is, is there a simple operation that would allow me to transform ar to ar2 using the vector I specified above? Thanks!

推荐答案

ar * rep(1:3, each=9) 应该可以...

对于任意大小的数组和任意一组乘法器,您知道数组的维度以及要执行元素乘法的轴(在本例中为 z 轴):

For an arbitrary sized array and an arbitrary set of multipliers, you know the dimensions of your array and the axis along which you want to perform your elementwise multiplication (in this case, the z axis):

each_arg <- prod(dim(ar)[1:2])
multipliers <- sample(1:10, 3)
ar2 <- ar * rep(multipliers, each=each_arg)

你也可以看看tensorA

这篇关于数组 - R 中的向量乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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