How to produce cartesian product in bash?
Combine two brace expansions! $ printf “%s\n” {1..3}” “{1..5} 1 1 1 2 1 3 1 4 1 5 2 1 2 2 2 3 2 4 2 5 3 1 3 2 3 3 3 4 3 5 This works by using a single brace expansion: $ echo {1..5} 1 2 3 4 5 … Read more
Combine two brace expansions! $ printf “%s\n” {1..3}” “{1..5} 1 1 1 2 1 3 1 4 1 5 2 1 2 2 2 3 2 4 2 5 3 1 3 2 3 3 3 4 3 5 This works by using a single brace expansion: $ echo {1..5} 1 2 3 4 5 … Read more
In Java terms, Scala’s Seq would be Java’s List, and Scala’s List would be Java’s LinkedList. Note that Seq is a trait, which is similar to Java’s interface, but with the equivalent of up-and-coming defender methods. Scala’s List is an abstract class that is extended by Nil and ::, which are the concrete implementations of … Read more
Use the length.out argument of rep() or rep_len (a “faster simplified version” [of rep]): length.out: non-negative integer. The desired length of the output vector Here is an example using the built-in dataset cars. str(cars) ‘data.frame’: 50 obs. of 2 variables: $ speed: num 4 4 7 7 8 9 10 10 10 11 … $ … Read more
Use sequence: > sequence(1:5) [1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
remove[c(TRUE, FALSE)] will do the trick. How it works? If logical vectors are used for indexing in R, their values are recycled if the index vector is shorter than the vector containing the values. Here, the vector remove contains ten values. If the index vector c(TRUE, FALSE) is used, the actual command is: remove[c(TRUE, FALSE, … Read more
You missed the each= argument to rep(): R> n <- 3 R> rep(1:5, each=n) [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 R> so your example can be done with a simple R> rep(1:8, each=20)
In bash, brace expansion happens before variable expansion, so this is not directly possible. Instead, use an arithmetic for loop: start=1 end=10 for ((i=start; i<=end; i++)) do echo “i: $i” done OUTPUT i: 1 i: 2 i: 3 i: 4 i: 5 i: 6 i: 7 i: 8 i: 9 i: 10