Fill matrix with path
2 answers
I see a few things here:
- You might want to set a random seed for replicability (
set.seed(20430)
). This means that every time you run the code, you get exactly the same set of pseudo-random variations. - Then your data will simply be independent; they won't actually have a multidimensional structure (although that might be what you want). In general, if you want to generate multidimensional data, you should use ? Mvrnorm , from MASS . (For more information, see here .)
- As a minor point, if you want the standard normal data, you do not need to specify
mu = 0
andsigma = 1
as these are the defaults forrnorm()
. - You don't need a loop to fill a matrix in R, just generate as many values as you want and add them directly using the argument
data=
in the functionmatrix()
. If you were really tied to using a loop, you should probably use a double loop so that you iterate over the columns and, within each loop, iterate over the rows. (Note that this is a very inefficient way to code in R - although I do it all the time ;-). - Finally, I cannot tell you what is
p
supposed to be done in your code.
Here's an easy way to do what you seem to be going to do:
set.seed(20430) n = 1000 k = 5 dat = rnorm(n*k) x = matrix(data=dat, nrow=n, ncol=k)
If you really want to use loops, you can do it like this:
mu = 0 sigma = 1 x = matrix(data=NA, nrow=n, ncol=k) for(j in 1:k){ for(i in 1:n){ x[i,j] = rnorm(1, mu, sigma) } }
+8
source to share
A=c(2,3,4,5);# In your case row terms
B=c(3,4,5,6);# In your case column terms
x=matrix(,nrow = length(A), ncol = length(B));
for (i in 1:length(A)){
for (j in 1:length(B)){
x[i,j]<-(A[i]*B[j])# do the similarity function, simi(A[i],B[j])
}
}
x # matrix is filled
I thought about my problem.
+1
source to share