[英]Values of matrix based on their column and row number in R
I have a 30 x 30 matrix in R and I want the values to be multiplied of their column and row number.我在 R 中有一个 30 x 30 矩阵,我希望将这些值乘以它们的列号和行号。 For example the first value is [1] * [1] = 1
例如第一个值为 [1] * [1] = 1
mat2 <- matrix(nrow= 30, ncol = 30)
We can use row
and col
to get the index of row/columns and use that to multiply我们可以使用
row
和col
来获取行/列的索引并使用它来相乘
mat2 <- row(mat2) *col(mat2)
If you know the size of the matrix, you can also use outer()
and construct the matrix directly in one step.如果你知道矩阵的大小,你也可以使用
outer()
,直接一步构建矩阵。
mat2 <- outer(seq(30), seq(30))
# other simple variations:
outer(1:30, 1:30)
seq(30) %o% seq(30)
1:30 %o% 1:30
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.