Select 矩阵中具有其他矩阵索引的元素
Select element in matrix with index from other matrix
我正在尝试 select 矩阵 z 中的元素和矩阵 y 中的索引。当像这样索引它时 z[1,y[1,1]]
它给出了另一个值而不是像这样索引它时 z[1,15]
(使用 y[1,1] = 15
)。怎么来的?
> z[1,y[1,1]]
[1] 15
> y[1,1]
[1] 15
> z[1,15]
6/09/2021
1: 2
这可能是由于四舍五入的差异。当您将计算结果存储到 y[1,1]
中时,可能会发生这种情况。
让我举个例子说明这种情况何时会发生。
mat <- matrix(1:10, ncol = 2)
print(mat)
# [,1] [,2]
# [1,] "a" "f"
# [2,] "b" "g"
# [3,] "c" "h"
# [4,] "d" "i"
# [5,] "e" "j"
ind <- exp(4) / 54.598152 * 2
print(ind)
# [1] 2
print(mat[1, ind]) # Extracts the value from the first column.
# [1] "a"
print(mat[1, round(ind)]) # Extracts the value from the second column.
# [1] "f"
因此,请尝试使用 z[1, round(y[1,1])]
检查问题是否仍然存在。
我正在尝试 select 矩阵 z 中的元素和矩阵 y 中的索引。当像这样索引它时 z[1,y[1,1]]
它给出了另一个值而不是像这样索引它时 z[1,15]
(使用 y[1,1] = 15
)。怎么来的?
> z[1,y[1,1]]
[1] 15
> y[1,1]
[1] 15
> z[1,15]
6/09/2021
1: 2
这可能是由于四舍五入的差异。当您将计算结果存储到 y[1,1]
中时,可能会发生这种情况。
让我举个例子说明这种情况何时会发生。
mat <- matrix(1:10, ncol = 2)
print(mat)
# [,1] [,2]
# [1,] "a" "f"
# [2,] "b" "g"
# [3,] "c" "h"
# [4,] "d" "i"
# [5,] "e" "j"
ind <- exp(4) / 54.598152 * 2
print(ind)
# [1] 2
print(mat[1, ind]) # Extracts the value from the first column.
# [1] "a"
print(mat[1, round(ind)]) # Extracts the value from the second column.
# [1] "f"
因此,请尝试使用 z[1, round(y[1,1])]
检查问题是否仍然存在。