r - How to generate a matrix with a sequence based on one m variable -
i have problem for
loop matrix 1 variable m
. how solve matrix loop?
[1 1/2 ... 1/(m+1), 1/2 1/3 ... 1/(m+2), ... ... ..., 1/(m+1) 1/(m+2) ... 1/(2m+1)]
i generate on matlab code:
m = 10; m1 = repmat(1:m+1,1,m+1); m2 = reshape(m1,m+1,m+1); m3 = m2 + m2' - 1; m4 = 1./m3;
how can generate same code in r?
you can do:
m <- 4 <- matrix(,m+1, m+1) <- 1/(col(a)+row(a)-1) # > # [,1] [,2] [,3] [,4] [,5] # [1,] 1.0000000 0.5000000 0.3333333 0.2500000 0.2000000 # [2,] 0.5000000 0.3333333 0.2500000 0.2000000 0.1666667 # [3,] 0.3333333 0.2500000 0.2000000 0.1666667 0.1428571 # [4,] 0.2500000 0.2000000 0.1666667 0.1428571 0.1250000 # [5,] 0.2000000 0.1666667 0.1428571 0.1250000 0.1111111
Comments
Post a Comment