R中数据滚动子集的矩阵代数

时间:2013-04-22 09:13:46

标签: r matrix

我是R的新手并且遇到以下问题:

数据输入

我有一个包含两列的CSV:

1,10
2,20
3,30
4,40
5,50
...
N, M

问题:

a) Read in the CSV
b) Convert to a matrix
c) Take 4 rows starting with the first row
d) calculate the third column of elements as a simple square of the second column elements
e) Calculate the sumproduct (value11*value12*value13 + value21*value22*value23 + value31*value32*value33 +value41*value42*value43)
f) Do this until the end of the dataset

结果:

前两个矩阵的结果应如下:

第一个矩阵:

从第一行开始,选择四行。

第1步

1,10
2,20
3,30
4,40

步骤2(将第3列添加为第二列值的平方)

1,10,100
2,20,400
3,30,900
4,40,1600

第3步(计算sumproduct)     (1 * 10 * 100 + 2 * 20 * 400 + 3 * 30 * 900 + 4 * 40 * 1600)== 354.000

第二个矩阵:

继续第二行并接下来的4行

第1步

2,20
3,30
4,40
5,50

步骤2(将第3列添加为第二列值的平方)

2,20,400
3,30,900
4,40,1600
5,50,2500

步骤3(计算sumproduct)

(2*20*400 +3*30*900 + 4*40*1600 + 5*50*2500) == 978.000

N矩阵 执行步骤1到3直到数据集结束

结果

结果应该是sumproducts列表:

354.000, 978.000, ..., N

谢谢!

0 个答案:

没有答案