我创建了两个矩阵如下:
A = c(1,2,3)
B = c(2,4,6)
c = as.matrix(c(3,6,9))
z = as.matrix(cbind(A, B))
Run Code Online (Sandbox Code Playgroud)
现在我想取矩阵c并逐行减去它,例如1-3 = -2和2-3 = -1对R编程有一个很好的理解,我想创建一个for循环.请你所有的答案都应该改善我的循环.
for (i in 1:nrow(z))# for the rows in matrix z
for (j in 1:nrow(c)) # for the rows in matrix c
{
sub = matrix(NA, 3,2) # make a placeholder
sub [i,]= z[i,]-c[j,] # i am not sure whether this right
return((sub))
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Error: no function to return from, jumping to top level
Run Code Online (Sandbox Code Playgroud)
我相信我的for循环是错误的,任何人都可以提供帮助.目的是了解有关R编程的更多信息.谢谢