how to generate all possible combination from a sequence

how to generate all possible combination from a n-dimensional vector without repetition of numbers.. i wanted combination of vec = 1:11;
eg:
vec = [1 2 3]
result = [
1 2 3;
1 3 2;
2 3 1;
2 1 3;
3 2 1;
3 1 2];

댓글 수: 1

Stephen23
Stephen23 2016년 5월 28일
편집: Stephen23 2016년 5월 28일
Your question is erroneous: these are not the combinations of vec, but are actually the permutations.
The difference is very simple:
  • if the order does not matter: combinations.
  • if the order is important: permutations.
You should make a complaint to your high school that they did not teach these basic mathematical terms correctly. Ask for your money back!

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2016년 5월 28일
>> perms(1:3)
ans =
3 2 1
3 1 2
2 3 1
2 1 3
1 2 3
1 3 2

댓글 수: 5

Elysi Cochin
Elysi Cochin 2016년 5월 28일
편집: Elysi Cochin 2016년 5월 28일
when i do perms(1:11), its showing
out of memory....
is there any way to solve it....
Matt J
Matt J 2016년 5월 28일
편집: Matt J 2016년 5월 28일
Computing the result as uint8
A=perms(uint8(1:11))
should reduce memory requirements to 0.4 GB for A. If you don't have that much, start clearing variables...
People don't realize just how easy it is to create a huge array.
There are factorial(11) permutations of a vector of length 11.
factorial(11)
ans =
39916800
But that forma an array with that many rows, but 11 columns.
factorial(11)*11
ans =
439084800
Stored in double precision, how any bytes?
factorial(11)*11*8
ans =
3512678400
So roughly 3.5 gigabytes. Stored as uint8 is better of course, but then you would ask next how to compute all permutations of a vector of length 12. :)
i tried A=perms(uint8(1:11)) still Out of memory
Stephen23
Stephen23 2016년 5월 30일
편집: Stephen23 2016년 5월 30일
@Elysi Cochin: essentially you have two choices:
  1. buy more memory for your computer.
  2. change your algorithm.
As the other commenters have already told you, even though beginners often imagine that their computer has never-ending memory and processing capabilities, in practice it is very simple to define a few commands that far exceed any computer's abilities.
Your task (as the author of your algorithm) is to understand this and to get information on alternatives. One possibility is to use a permutation generator that generates each permutation at a time (not all at once). This will be slow, but it will avoid the memory problems. You will find several submissions on FEX that provide this functionality:
Ultimately however, you will just find that this takes too long, and you will then have to consider alternatives to your algorithm. Good luck!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Elementary Math에 대해 자세히 알아보기

질문:

2016년 5월 28일

편집:

2016년 5월 30일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by