A. T
A. T

Reputation: 1834

Swapping elements of vectors in julia

Let A=[[(a,b),(c,d),(e,f),(g,h)]]

I want to have all swapping possible using for loop for example:

A=[[ (c,d),(a,b),(e,f),(g,h)]]
A=[[ (a,b),(e,f),(c,d),(g,h)]]

etc...

I can swap two elements but not the whole element as above.

Upvotes: 2

Views: 427

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69839

Is this what you want?

julia> using Combinatorics

julia> x = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> collect(permutations(x))
6-element Vector{Vector{Int64}}:
 [1, 2, 3]
 [1, 3, 2]
 [2, 1, 3]
 [2, 3, 1]
 [3, 1, 2]
 [3, 2, 1]

Upvotes: 1

Related Questions