combinations

combinations(array, n)

Generate all combinations of n elements from an indexable object. Because the number of combinations can be very large, this function returns an iterator object. Use collect(combinations(array,n)) to get an array of all combinations.

Examples

In the Julia programming language, the function combinations(array, n)

Generate all combinations of n elements from an indexable object. Because the number of combinations can be very large, this function returns an iterator object. Use collect(combinations(array,n)) to get an array of all combinations.

julia> collect(combinations([1, 2, 3], 2))
3-element Array{Tuple{Int64,Int64},1}:
 (1, 2)
 (1, 3)
 (2, 3)

This example generates all combinations of 2 elements from the array [1, 2, 3] and collects them into an array.

julia> collect(combinations("abc", 2))
3-element Array{Tuple{Char,Char},1}:
 ('a', 'b')
 ('a', 'c')
 ('b', 'c')

It generates all combinations of 2 characters from the string "abc" and collects them into an array.

Common mistake example:

julia> collect(combinations([1, 2, 3], 4))
0-element Array{Tuple{Int64,Int64,Int64,Int64},1}

In this example, the value of n is larger than the number of elements in the array. As a result, there are no combinations to generate. Make sure n is within the range of the number of elements in the array to avoid such errors.

See Also

Array, broadcast, cat, combinations, conj!, digits!, fieldnames, fill, fill!, last, length, maximum, minimum, ones, parent, parentindexes, partitions, permutations, pointer, pointer_to_array, promote_shape, rand!, reshape, scale, similar, sum, sum_kbn, takebuf_array, transpose!, vec, zeros,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: