向量在对某一个数x进行素数分解后包含以下数值数据:
c <- c(2,2,2,3)
这是24的素因式分解。
矩阵的第一列应包含素数因子2和3,第二列应包含素数因子的幂。
例如:
| 2 3 |
| 3 1 |
我该如何创建这个矩阵。
发布于 2022-10-19 11:05:51
用tabulate
f <- as.integer(gmp::factorize(24))
cnts <- tabulate(f)[-1]
matrix(c(unique(f), cnts[cnts > 0L]), ncol = 2)
#> [,1] [,2]
#> [1,] 2 3
#> [2,] 3 1
或使用table
f <- table(as.integer(gmp::factorize(24)))
matrix(c(as.integer(names(f)), f), ncol = 2)
#> [,1] [,2]
#> [1,] 2 3
#> [2,] 3 1
https://stackoverflow.com/questions/74124059
复制相似问题