Reputation: 161
I have a list named "dahak" that contains 30000 number between 1 to 10. I want to check every number with all of the number in the list, if two numbers are equals then append number 1 to weight_list, if two numbers are not equals then calculate their difference and store it as x and append x to the weight_list. Here is the code:
for(j in 1:num_nodes){
for (k in 1:num_nodes){
if(j==k){
weight_list <- c(weight_list,0)
}
else if(as.numeric(dahak[j])==as.numeric(dahak[k])){
weight_list <- c(weight_list,1)
}
else if(as.numeric(dahak[j])!=as.numeric(dahak[k])){
x = 1 - (abs(as.numeric(dahak[j]) - as.numeric(dahak[k])) / 10)
weight_list <- c(weight_list,x)
}
}
}
How can i optimize this code? and how can i do this with lapply?
Upvotes: 2
Views: 98
Reputation: 101403
I guess this might be the simplification you are looking for, where outer
and ifelse
are used.
Below is an example with dummy data:
set.seed(1)
num_nodes <- 15
dahak <- sample(10,num_nodes,replace = TRUE)
If you want a matrix for weigth_list
of dimensions num_nodes
, then you can try
weight_list <- (u<-ifelse((z<-abs(outer(dahak,dahak,FUN = "-")))!=0,1-z/10,1))-diag(diag(u))
such that
> weight_list
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,] 0.0 0.5 0.8 0.2 0.3 0.8 0.3 0.4 0.2 0.6 0.6 0.9 0.7 0.9 0.8
[2,] 0.5 0.0 0.7 0.7 0.8 0.7 0.8 0.9 0.7 0.9 0.9 0.4 0.8 0.4 0.7
[3,] 0.8 0.7 0.0 0.4 0.5 1.0 0.5 0.6 0.4 0.8 0.8 0.7 0.9 0.7 1.0
[4,] 0.2 0.7 0.4 0.0 0.9 0.4 0.9 0.8 1.0 0.6 0.6 0.1 0.5 0.1 0.4
[5,] 0.3 0.8 0.5 0.9 0.0 0.5 1.0 0.9 0.9 0.7 0.7 0.2 0.6 0.2 0.5
[6,] 0.8 0.7 1.0 0.4 0.5 0.0 0.5 0.6 0.4 0.8 0.8 0.7 0.9 0.7 1.0
[7,] 0.3 0.8 0.5 0.9 1.0 0.5 0.0 0.9 0.9 0.7 0.7 0.2 0.6 0.2 0.5
[8,] 0.4 0.9 0.6 0.8 0.9 0.6 0.9 0.0 0.8 0.8 0.8 0.3 0.7 0.3 0.6
[9,] 0.2 0.7 0.4 1.0 0.9 0.4 0.9 0.8 0.0 0.6 0.6 0.1 0.5 0.1 0.4
[10,] 0.6 0.9 0.8 0.6 0.7 0.8 0.7 0.8 0.6 0.0 1.0 0.5 0.9 0.5 0.8
[11,] 0.6 0.9 0.8 0.6 0.7 0.8 0.7 0.8 0.6 1.0 0.0 0.5 0.9 0.5 0.8
[12,] 0.9 0.4 0.7 0.1 0.2 0.7 0.2 0.3 0.1 0.5 0.5 0.0 0.6 1.0 0.7
[13,] 0.7 0.8 0.9 0.5 0.6 0.9 0.6 0.7 0.5 0.9 0.9 0.6 0.0 0.6 0.9
[14,] 0.9 0.4 0.7 0.1 0.2 0.7 0.2 0.3 0.1 0.5 0.5 1.0 0.6 0.0 0.7
[15,] 0.8 0.7 1.0 0.4 0.5 1.0 0.5 0.6 0.4 0.8 0.8 0.7 0.9 0.7 0.0
Upvotes: 0
Reputation: 173858
It sounds like you want to create a 30,000 x 30,000 matrix. It also sounds like dahak
is a vector rather than a list. If that's really what you want to do, you can simplify your logic and vectorize like this;
get_weights <- function(x) 1 - abs(x - as.numeric(dahak))/10
weights <- do.call(rbind, lapply(as.numeric(dahak), get_weights)) - diag(length(dahak))
Using the same dummy data as @ThomasIsCoding I get:
weights
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
#> [1,] 0.0 0.9 0.7 0.3 1.0 0.4 0.3 0.6 0.6 0.8 1.0 0.9 0.6 0.9 0.5
#> [2,] 0.9 0.0 0.8 0.4 0.9 0.5 0.4 0.7 0.7 0.7 0.9 0.8 0.7 1.0 0.6
#> [3,] 0.7 0.8 0.0 0.6 0.7 0.7 0.6 0.9 0.9 0.5 0.7 0.6 0.9 0.8 0.8
#> [4,] 0.3 0.4 0.6 0.0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