R,遍历矩阵的行向量 [英] R, iterating over the row vectors of a matrix
本文介绍了R,遍历矩阵的行向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些向量vect
,我想遍历矩阵的行向量v
并计算:
I have some vector vect
and I want to iterate over the row vectors v
of a matrix and calculate:
cov(v, vect)
.
我尝试过:
for(vect in mat2) #where mat2 is a 215 by 31 matrix
但是,每个向量似乎都是一个值为1的标量.
However, each vector appeared to be a scalar with value 1.
- 如何迭代矩阵的行向量?
- 为了使这个更好,因为我有兴趣计算
cov(v, vect)
的和,其中v是行向量,我该如何使用高阶函数left-fold
和right-fold
- How do I iterate over the row vectors of a matrix?
- To make this even better, since I am interested in calculating the sum of
cov(v, vect)
where v is a row vector, how can I use the higher-order functionsleft-fold
andright-fold
推荐答案
您在寻找apply
吗?
apply(mat2, 1, function(v)cov(v,vect))
这篇关于R,遍历矩阵的行向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文