Displaying 4 results from an estimated 4 matches for "s_diff3".
Did you mean:
s_diff2
2024 Jun 16
1
slowness when I use a list comprehension
...s <- 0
for( i in 1:length(vec2)){
sum_squares <- (
sum_squares
+ (
vec1[ (i-1)*ratio_sampling+j ]
- vec2[i]
)**2
)
}
S_diff2[j] <- sum_squares
}
S_diff2
}
vloop <- function( N1, M2, ratio_sampling, vec1, vec2 ) {
S_diff3 <- numeric(
N1-(N2-1)*ratio_sampling
)
i <- seq_along( vec2 )
k <- (i-1)*ratio_sampling
for( j in seq_along( S_diff3 ) ) {
S_diff3[j] <- sum(
(
vec1[ j + k ]
- vec2
)^2
)
}
S_diff3
}
microbenchmark(
S_diff2 <- dloop( N1, M2, rati...
2024 Jun 16
1
slowness when I use a list comprehension
...s <- 0
for( i in 1:length(vec2)){
sum_squares <- (
sum_squares
+ (
vec1[ (i-1)*ratio_sampling+j ]
- vec2[i]
)**2
)
}
S_diff2[j] <- sum_squares
}
S_diff2
}
vloop <- function( N1, M2, ratio_sampling, vec1, vec2 ) {
S_diff3 <- numeric(
N1-(N2-1)*ratio_sampling
)
i <- seq_along( vec2 )
k <- (i-1)*ratio_sampling
for( j in seq_along( S_diff3 ) ) {
S_diff3[j] <- sum(
(
vec1[ j + k ]
- vec2
)^2
)
}
S_diff3
}
microbenchmark(
S_diff2 <- dloop( N1, M2, rati...
2024 Jun 16
1
slowness when I use a list comprehension
Laurent,
Thank you for introducing me to a package I did not know existed as I use features like list comprehension in python all the time and could see using it in R now that I know it is available.
As to why you see your example as slow, I see you used a fairly complex and nested expression and wonder if it was a better way to go. As you are dealing with an interpreter doing delayed
2024 Jun 16
1
slowness when I use a list comprehension
This can be vectorized. Try
ix <- seq_along(vec2)
S_diff2 <- sapply(seq_len(N1-(N2-1)*ratio_sampling), \(j)
sum((vec1[(ix-1)*ratio_sampling+j] - vec2[ix])**2))
On Sun, Jun 16, 2024 at 11:27?AM Laurent Rhelp <laurentRHelp at free.fr> wrote:
>
> Dear RHelp-list,
>
> I try to use the package comprehenr to replace a for loop by a list
> comprehension.
>
> I