MATLAB Answers

how to get the all possible combination between two range of numbers

8 ビュー (過去 30 日間)
RAVI
RAVI 2018 年 6 月 29 日
コメント済み: Steven Lord 2018 年 6 月 29 日
every time this code iterate random number between 1 to 4. for 1st iteration c=2 and g=2. for second iteration c=1 and g=2; i need never the combination repeated once it comes
xx=4
c= randi(length(xx))
g= randi(length(xx))

  2 件のコメント

Stephen Cobeldick
Stephen Cobeldick 2018 年 6 月 29 日
xx=4
c= randi(length(xx))
g= randi(length(xx))
xx is scalar, so length(xx) is 1, which means both randi calls will always produce the same output:
>> xx = 4;
>> randi(length(xx))
ans = 1
>> randi(length(xx))
ans = 1
>> randi(length(xx))
ans = 1
>> randi(length(xx))
ans = 1
>> randi(length(xx))
ans = 1
>> randi(length(xx))
ans = 1
>> randi(length(xx))
ans = 1
... etc
Is this the intended behavior?
RAVI
RAVI 2018 年 6 月 29 日
xx=1:4

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

採用された回答

KL
KL 2018 年 6 月 29 日
The example and description is a but confusing but if you ask how to get the all possible combination between two range of numbers, use perms
my_range = 1:4; %or even [1 3 5]...
c = perms(my_range)

  4 件のコメント

表示 1 件の古いコメント
KL
KL 2018 年 6 月 29 日
Hmmm.. I'm still not very sure but is this what you want,
c = repmat(1:4,4,1);
g = repmat(1:4,1,4);
x = [c(:) g(:)]
RAVI
RAVI 2018 年 6 月 29 日
thanks.....this works....but i need to know is there any function in matlab to do so as our number of variable(c,g,etc) increases....
Steven Lord
Steven Lord 2018 年 6 月 29 日
How many variables are you planning to use and how many values will each variable be able to take on? If you want to solve with too many variables or with variables that can take on too many different values, you may need to consider a new approach for solving your problem that avoids combinatorial explosion.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by