programming to find out all possible combination of numbers a,b,c,d in which 0<a<b<c<d<90.
조회 수: 2 (최근 30일)
이전 댓글 표시
all possible combination of numbers a,b,c,d in which 0<a<b<c<d<90.
댓글 수: 2
Guillaume
2015년 8월 26일
You have to clarify what you mean by numbers. There is of course an infinity of numbers between 0 and 90. Do you mean integers?
채택된 답변
Guillaume
2015년 8월 26일
편집: Guillaume
2015년 8월 26일
Isn't this simply four for loop?
for a = 1:86
for b = a+1:87
for c = b+1:88
for d = c+1:89
%do whatever you want with a,b,c,d
end
end
end
end
댓글 수: 3
Walter Roberson
2015년 9월 4일
Be specific about how you want them output. Do you want them all displayed? If so then is a list of 4 numbers per line acceptable? Do you need every line to include the text 'a=' and ',b=' and so on? Do you want a variable that contains a something-by-4 array where each line is an a, b, c, d selection?
추가 답변 (4개)
Walter Roberson
2015년 8월 26일
There will be 4636033603912859644 choices for "a" alone, for a total of 19247532396881346240525890574203961674141911582083171582958740223586992125 combinations. That is roughly the cube of the number of fundamental particles in the observable universe, so there is no known means for recording all of those combinations.
댓글 수: 8
Walter Roberson
2015년 8월 27일
A = ABCD(:, 1); B = ABCD(:, 2);
etc. The 5th solution would be A(5),B(5),C(5),D(5)
You asked for all of the solutions and this is all of the solutions. Millions of them.
Kelly Kearney
2015년 8월 27일
For just the integers:
tmp = nchoosek(1:89,4);
a = tmp(:,1);
b = tmp(:,2);
c = tmp(:,3);
d = tmp(:,4);
The nchoosek function automatically sorts its results, so you don't have to worry about the inequality check. You could use the same function to test non-integers, too, though as everyone has pointed out the size of your arrays will quickly get out of control as you increase resolution.
댓글 수: 1
Roger Stafford
2015년 9월 4일
Kelly's answer using 'nchoosek' is the best one, Ujjwal, and you should accept it. It will give you 89!/4!/85! = 2,441,626 possible combinations.
Note however that he misstates things a bit where he asserts that 'nchoosek' sorts the results. It does not. It actually uses the same order as was present in the received vector argument, which in the case of 1:89 would happen to be sorted.
UJJWAL BARMAN
2015년 9월 4일
댓글 수: 1
Guillaume
2015년 9월 4일
Please use comments rather than answering your own question with another question.
To solve your problem, simply move the save after the end of the last loop and save aa, bb, etc. instead of a, b, etc.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!