for scientific purposes I have to evaluate a function of 8 parameters over and over again for these parameters that take values in some ranges.
Is there something more clever (and better performing) than a straightforward code with 8 nested for loops?
thanks
You can use Iterators.product, which combine multiple iterators, like this:
julia> for (i, j, k) in Iterators.product(1:2, 5:6, -2:-1)
println("hi, $i, $j and $(k)!")
end
hi, 1, 5 and -2!
hi, 2, 5 and -2!
hi, 1, 6 and -2!
hi, 2, 6 and -2!
hi, 1, 5 and -1!
hi, 2, 5 and -1!
hi, 1, 6 and -1!
hi, 2, 6 and -1!
You can also return it as a tuple, that you index into, like this:
julia> for K in Iterators.product(1:2, 5:6, -2:-1)
println("hi, $(K[1]), $(K[2]) and $(K[3])!")
end
hi, 1, 5 and -2!
hi, 2, 5 and -2!
hi, 1, 6 and -2!
hi, 2, 6 and -2!
hi, 1, 5 and -1!
hi, 2, 5 and -1!
hi, 1, 6 and -1!
hi, 2, 6 and -1!
Related
I need help regarding my dataset below.
I have the below dataset and want to filter the same value that happens sequentially for each ID, and just keep only a single value. Thanks for your help
ID, Value
1, 9
1, 10
1, 10
1, 9
1, 13
2, 9
2, 8
2, 8
2, 9
2, 5
The result should be as follows:
ID, Value
1, 9
1, 10
1, 9
1, 13
2, 9
2, 8
2, 9
2, 5
I have two vectors
a <- c(1, 5, 2, 1, 2, 3, 3, 4, 5, 1, 2)
b <- (1, 2, 3, 4, 5, 6)
I want to know how many times each element in b occurs in a. So the result should be
c(3, 3, 2, 1, 2, 0)
All methods I found like match(),==, %in% etc. are not suited for entire vectors. I know I can use a loop over all elements in b,
for (i in 1:length(b)) {
c[I] <- sum(a==b, na.rm=TRUE)
}
but this is used often and takes to long. That's why I'm looking for a vectorized way, or a way to use apply().
You can do this using factor and table
table(factor(a, unique(b)))
#
#1 2 3 4 5 6
#3 3 2 1 2 0
Since you mentioned match, here is a possibility without sapply loop (thanks to #thelatemail)
table(factor(match(a, b), unique(b)))
#
#1 2 3 4 5 6
#3 3 2 1 2 0
Here is a base R option, using sapply with which:
a <- c(1, 5, 2, 1, 2, 3, 3, 4, 5, 1, 2)
b <- c(1, 2, 3, 4, 5, 6)
sapply(b, function(x) length(which(a == x)))
[1] 3 3 2 1 2 0
Demo
Here is a vectorised method
x = expand.grid(b,a)
rowSums( matrix(x$Var1 == x$Var2, nrow = length(b)))
# [1] 3 3 2 1 2 0
I am working on a list object containing hundreds of "lists" of random integers in the following format:
assignments <- list(
as.integer(c(1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3)),
as.integer(c(1, 1, 1, 0, 0, 0, 3, 3)),
as.integer(c(1, 3, 3, 3, 3, 3, 3, 2, 2)),
as.integer(c(1, 2, 0, 3, 2, 3, 2, 2, 2))
)
[[1]]
[1] 1 1 1 1 1 1 2 2 2 3 3
[[2]]
[1] 1 1 1 0 0 0 3 3
[[3]]
[1] 1 3 3 3 3 3 3 2 2
[[4]]
[1] 1 2 0 3 2 3 2 2 2
from which to extract the most frequent "non-zero" integer from a given list. However, in some lists of this list object, zero appears to be the most frequent integer, such as the second list [[2]]. The created some problems on my analysis.
Is there anyway to loop through a list of lists to remove certain elements, such as zero, from each list of this big list?
One method I've experimented earlier was to loop through this list of lists and use != to exclude values that equal zero
for(i in assignments){i[i != 0]}
but this didn't work.
lapply(assignments,function(x) x[x!=0])
I have bunch of observations
x = c(1, 2, 4, 1, 6, 7, 11, 11, 12, 13, 14)
that I want to turn into the group:
y = c(1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3)
I.e I want the first 5 integers (1 to 5) to constitute one group, the next 5 integers to constitute the next group (6 to 10), and so on.
Is there a straightforward way to accomplish this without a loop?
Clarification: I need to programmatically create the groups form the input vector (x)
We can use %/% to create the group
x%/%5+1
#[1] 1 1 1 1 2 2 3 3 3 3 3
You can use ceiling to create groups
ceiling(x/5)
# [1] 1 1 1 1 2 2 3 3 3 3 3
I have a vector c(9,6,3,4,2,1,5,7,8), and I want to switch the elements at index 2 and at index 5 in the vector. However, I don't want to have to create a temporary variable and would like to make the switch in one call. How would I do that?
How about just x[c(i,j)] <- x[c(j,i)]? Similar to replace(...), but perhaps a bit simpler.
swtch <- function(x,i,j) {x[c(i,j)] <- x[c(j,i)]; x}
swtch(c(9,6,3,4,2,1,5,7,8) , 2,5)
# [1] 9 2 3 4 6 1 5 7 8
You could use replace().
x <- c(9, 6, 3, 4, 2, 1, 5, 7, 8)
replace(x, c(2, 5), x[c(5, 2)])
# [1] 9 2 3 4 6 1 5 7 8
And if you don't even want to assign x, you can use
replace(
c(9, 6, 3, 4, 2, 1, 5, 7, 8),
c(2, 5),
c(9, 6, 3, 4, 2, 1, 5, 7, 8)[c(5, 2)]
)
# [1] 9 2 3 4 6 1 5 7 8
but that's a bit silly. You will probably want x assigned to begin with.
If you actually want to do it without creating a temporary copy of the vector, you would need to write a short C function.
library(inline)
swap <- cfunction(c(i = "integer", j = "integer", vec="integer"),"
int *v = INTEGER(vec);
int ii = INTEGER(i)[0]-1, jj = INTEGER(j)[0]-1;
int tmp = v[ii];
v[ii] = v[jj];
v[jj] = tmp;
return R_NilValue;
")
vec <- as.integer(c(9,6,3,4,2,1,5,7,8))
swap(2L, 5L, vec)
vec
# [1] 9 2 3 4 6 1 5 7 8