R,遍历矩阵的行向量 [英] R, iterating over the row vectors of a matrix

查看:245
本文介绍了R,遍历矩阵的行向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些向量vect,我想遍历矩阵的行向量v并计算:

I have some vector vect and I want to iterate over the row vectors vof 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.

  1. 如何迭代矩阵的行向量?
  2. 为了使这个更好,因为我有兴趣计算cov(v, vect)的和,其中v是行向量,我该如何使用高阶函数left-foldright-fold
  1. How do I iterate over the row vectors of a matrix?
  2. 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 functions left-fold and right-fold

推荐答案

您在寻找apply吗?

apply(mat2, 1, function(v)cov(v,vect))

这篇关于R,遍历矩阵的行向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆