矩阵相减或相除时出现“不合格数组”错误

祖杰

我有一个5x3矩阵,称为matrixoutput

matrixoutput <- structure(c(755.012517738809, 713.680227809506, 796.559832334474, 
720.586278415567, 813.656728335122, 747.228849872966, 716.763851131365, 
790.005405393554, 719.099072835892, 794.80633176412, 747.924859415065, 
714.405749195011, 792.123774606548, 719.75129988389, 793.458302292789
), .Dim = c(5L, 3L))

我有一个名为5x1的矩阵matrixactual

matrixactual <- structure(c(743.2, 710.37, 787.77, 721.41, 808), .Dim = c(5L, 
1L))

我正在尝试matrixoutput为这样的每一列运行“ MAPE”计算

MAPE <- mean(abs((matrixactual-matrixoutput)/matrixactual) * 100)

但是,这将返回错误:

矩阵实际中的错误-矩阵输出:不合格数组

有解决方法的想法吗?我想有一个简单的解决方法。

李哲源
matrixactual <- c(matrixactual)  ## I mean `base::c`
colMeans(abs((matrixactual - matrixoutput) / matrixactual) * 100)

“循环规则”的作品背后的"-""/"


有时我们可能会遇到更加模糊的情况:

A <- matrix(1:15, 5, 3)
b <- array(1:5, dim = 5)
b
#[1] 1 2 3 4 5

b看起来像矢量,但实际上不是。它是具有“ dim”属性的一维数组。"-"会抱怨尺寸不一致。

A - b
#Error in A - b : non-conformable arrays

使用A - c(b)实际使用“循环规则”。

而且,"%*%"会失败。

A %*% b
#Error in A %*% b : non-conformable arguments

解决方法是A %*% c(b)

一维数组table在单个输入上使用时函数返回因此,我在这里只想说一句:也许健壮的代码段应始终检查维度。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章