的n减去矩阵,从N,K维矩阵的列K尺寸 [英] Subtract matrix of n,k dimensions from array of matrices of n,k dimensions
本文介绍了的n减去矩阵,从N,K维矩阵的列K尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个数组A
A <- array(0, c(4, 3, 5))
for(i in 1:5) {
set.seed(i)
A[, , i] <- matrix(rnorm(12), 4, 3)
}
如果我有矩阵B
and if I have matrix B
set.seed(6)
B <- matrix(rnorm(12), 4, 3)
在code到数组中的一个会的每个矩阵减去B:
The code to subtract B from the each matrix of the array A would be:
d<-array(0, c(4,3,5))
for(i in 1:5){
d[,,i]<-A[,,i]-B
}
不过,这将是code执行从使用功能相同的计算应用的家庭?
However, what would be the code to perform the same calculation using a function from "apply" family?
推荐答案
这是什么扫描
是
sweep(A, 1:2, B)
这篇关于的n减去矩阵,从N,K维矩阵的列K尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文