programming to find out all possible combination of numbers a,b,c,d in which 0<a<b<c<d<90.

all possible combination of numbers a,b,c,d in which 0<a<b<c<d<90.

2 件のコメント

Guillaume
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?
UJJWAL BARMAN
UJJWAL BARMAN 2015 年 8 月 26 日
I only want to get the integer values.the program will just have to deal with 1,2,3,4,..upto 89 these values only.The output will be like this. a=1 b=2 c=3 d=4 and goes on just only follow the condition that 0<a<b<c<d<90. I want all the possible combination.

サインインしてコメントする。

 採用された回答

Guillaume
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
Walter Roberson 2015 年 8 月 26 日
That only deals with integers. The past questions of the poster suggest that the poster intends to test all possible double precision numbers over that range, in order to attempt to find the solution to a set of simultaneous equations in 4 unknowns. (If the simultaneous equations have not changed since the earlier postings, my tests suggest that there are no solutions in the target range.)
UJJWAL BARMAN
UJJWAL BARMAN 2015 年 9 月 4 日
i want all the combination in output but it is giving only the last combination output i.e a=86,b=87,c=88,d=89.
Walter Roberson
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
Walter Roberson 2015 年 8 月 26 日

1 投票

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 件のコメント

Guillaume
Guillaume 2015 年 8 月 26 日
Am I missing something? Even if you remove the ordering relationship, all combinations of four integers between 1 and 89 is only 89^4 = 62742241 combinations.
Walter Roberson
Walter Roberson 2015 年 8 月 26 日
You are assuming integers. I, though, have answered several questions from the same poster in which the poster has been trying to find four angles in the range 0 to 90 which satisfy a particular set of simultaneous equations. (My investigations at the time suggested firmly that there were no real-valued solutions in that range -- though there were solutions if at least one or more of the angles was allowed to exceed that range.)
Guillaume
Guillaume 2015 年 8 月 26 日
Oh yes, if it's not integer, then of course the question makes no sense.
John D'Errico
John D'Errico 2015 年 8 月 26 日
Well, you cannot really say there is no known means. Simply listing the combinations in theory is adequate. It just might take a wee bit more time than most people are willing to invest.
Walter Roberson
Walter Roberson 2015 年 8 月 26 日
There isn't enough energy in the universe to be able to output them all on any known display.
[A,B,C,D] = ndgrid(1:86, 2:87, 3:88, 4:89);
ABCD = [A(:), B(:), C(:), D(:)];
idx = ABCD(:,1) < ABCD(:,2);
ABCD = ABCD(idx,:);
idx = ABCD(:,2) < ABCD(:,3);
ABCD = ABCD(idx,:);
idx = ABCD(:,3) < ABCD(:,4);
ABCD = ABCD(idx,:);
Now ABCD will be an N x 4 array in which column 1 corresponds to an A value, column 2 to a B value, column 3 to a C value, column 4 to a D value.
UJJWAL BARMAN
UJJWAL BARMAN 2015 年 8 月 27 日
explain what to do with : .....having difficulty with this.......
Walter Roberson
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
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
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
UJJWAL BARMAN 2015 年 9 月 4 日

0 投票

i am attaching a programming file that i have done which has runned sucessfully but i am having problem to save the output value of all the combination.please suggest how to save all the output data.

1 件のコメント

Guillaume
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.

サインインしてコメントする。

質問済み:

2015 年 8 月 26 日

回答済み:

2015 年 9 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by