I am trying to make my matrix (tc) symmetric (using R) by adding the corresponding entries and divide those by the sum of the corresponding diagonal entries (tc[i,j]+tc[j,i])/(tc[i,i]+tc[j,j]). I tried it with loops but it does not give me the right values let alone make the matrix symmetric. This is my code so far:
for (i in 1:end){
for(j in 1:end){
tc[i,j]<-(tc[i,j]+tc[j,i])/(tc[i,i]+tc[j,j])
}
}
It's probably a super obvious mistake but I can't figure it out. Can anyone help me? =)
Well, if you think about it, you are summing using values that you have already updated (since you are looping over each i and j).
What if you make a new matrix with the same dimensions as tc, and then run your loop.
newTc <- matrix(0, nrow=nrow(tc), ncol=ncol(tc))
for (i in 1:end){
for(j in 1:end){
newTc[i,j]<-(tc[i,j]+tc[j,i])/(tc[i,i]+tc[j,j])
}
}
Related
I am trying to figure out how to use a for loop to create a matrix of expected values. it should be able to handle a matrix of any size. This is all I've been able to come up with so far.
for(i in 1:obsv){
for(j in 1:obsv){
obsv[i,j]<-(sum(obsv[i,])*sum(obsv[,j]))/sum(obsv)
}
}
##obsv is the name of the matrix of observed values
Your loop is obviously wrong, see below. The main error was that you need to loop through 1:nrow(obsv) and 1:ncol(obsv), not like you are doing it.
I will use a fake matrix, since you haven't posted an example dataset.
obsv <- matrix(1:25, ncol = 5)
obsv2 <- obsv # modify a copy
for(i in 1:nrow(obsv)){
for(j in 1:ncol(obsv)){
obsv2[i, j] <- sum(obsv[i, ])*sum(obsv[, j])/sum(obsv)
}
}
Now, the above code can be greatly simplified. A one-liner will do it.
obsv3 <- rowSums(obsv) %*% t(colSums(obsv))/sum(obsv)
identical(obsv2, obsv3)
#[1] TRUE
I want to make matrices without using loops such as for , while.
So I tried assigned k and put k in function which makes matrices.
powlist= function(base,startnum,endnum) (base)^(startnum:endnum)
m_maker= function(base) matrix(c(powlist(base,0,19)),4,5)
k= 2:10
a= m_maker((k-1)/k)
But function returns only one matrix.
I think function should return 9 matrices.
Please let me know how should I change this code.
I want to make each matrices that first one is matrix m_maker(1/2) and
second one m_maker(2/3) so on.
When I put k=2 and k=3 each time, it returns what I want.
What I want is way to return 9 matrices at one to go.
You're looking for lapply, like
res <- lapply((k-1)/k, m_maker)
However, you really should use an array for something like this.
ares <- abind(res, along=3)
I have a fairly simply computation I need to do, but I cannot figure out how to do it in a way that is even close to efficient. I have a large nxn matrix, and I need to compute the following:
I'm still fairly inexperienced at coding, and so the only way that comes to my mind is to do the straightforward thing and use 3 for loops to move across the indexes:
sum=0
for(i in 1:n)
{
for(j in 1:n)
{
for(k in 1:n)
{
sum = sum + A[i,j]*A[j,k]
}
}
}
Needless to say, for any decent size matrix this takes forever to run. I know there must be a better, more efficient way to do this, but I cannot figure it out.
If you don't consider the k and i sums, you can realise that you are just doing the matrix product of A with itself. Such product in R is obtained through the %*% operator. After calculating this matrix, you just need to sum all the elements together:
sum(A %*% A)
should give the result you are seeking.
I am a beginner in R and i know the way i have done is wrong and slow. I would like to fill a matrix and i need to compute each term. I have tried two for loops, here is the code. Do you know a better way to do it?
KernelGaussianMatrix <- function(x,delta){
Mat = matrix(0,nrow=length(x),ncol=length(x))
for (i in 1:length(x)){
for (j in 1:length(x)){
Mat[i,j] = KernelGaussian(x[i],x[j],delta)
}
}
return(Mat)
}
Thx
you want to use the function outer as in:
Mat <- outer(x,x,KernelGaussian,delta)
note that any arguments after the third argument in outer are provided as additional arguments to the function provided as the third argument to outer
If a for loop is required to generate the values than your method is fine.
If the values are already in an array values you can try mat = matrix(values, nrow=n, ncol=p) or something similar.
I want to make a loop which contains two variables i,j. for each i equals 1:24, j can be 1:24
but I don't know to make this loop;
i=1
while(i<=24)
{
j=seq(1,24,by=1)
for (j in j)
{
cor[i,j]
}
}
i=i+1
is this right? my output is cor[i,j].
In order to accomplish your final goal try...
cor(myMatrix)
The result is a matrix containing all of the correlations of all of the columns in myMatrix.
If you want to try to go about it the way you were it's probably best to generate a matrix of all of the possible combinations of your items using combn. Try combn(1:4,2) and see what it looks like for a small example. For your example with 24 columns the best way to cycle through all combinations using a for loop is...
myMatrix <- matrix(rnorm(240), ncol = 24)
myIndex <- combn(1:24,2)
for(i in ncol(myIndex)){
temp <- cor(myMatrix[,myIndex[1,i]],myMatrix[,myIndex[2,i]])
print(c(myIndex[,i],temp))
}
So, it's possible to do it with a for loop in R you'd never do it that way.
(and this whole answer is based on a wild guess about what you're actually trying to accomplish because the question, and your comments, are very hard to figure out)