putting the combinations of a sequence in an cell array

13 Ansichten (letzte 30 Tage)
N/A
N/A am 24 Sep. 2019
Bearbeitet: Stephen23 am 24 Sep. 2019
AllCombinations = {1 2 3 4 5 [1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5] [1 2 3] [1 2 4] [1 2 5] [1 3 4] [1 3 5] [1 4 5] [2 3 4] [2 3 5] [2 4 5] [3 4 5] [1 2 3 4] [1 2 3 5] [1 2 4 5] [1 3 4 5] [2 3 4 5] [1 2 3 4 5] }
A =[num2cell(combnk(1:5, 1),2)' [num2cell(combnk(1:5, 2),2)'[num2cell(combnk(1:5, 3),2)'[num2cell(combnk(1:5, 4),2)'[num2cell(combnk(1:5, 5),2)']
Goal:
AllCombinations = {1 2 3 4 5 [1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5] [1 2 3] [1 2 4] [1 2 5] [1 3 4] [1 3 5] [1 4 5] [2 3 4] [2 3 5] [2 4 5] [3 4 5] [1 2 3 4] [1 2 3 5] [1 2 4 5] [1 3 4 5] [2 3 4 5] [1 2 3 4 5] }
generate a cell array which is similair to this, but without entering the values manually
I have a set [1 2 3 4 5] and I have to generate all possible combinations and put it in a cell array, such that comk{6} returns [1 2]. Hence order is also important.
I personally thought about using a loop to get something like this, but i am not sure how the loop should be constructed.
A =[num2cell(combnk(1:5, 1),2)' ..... [num2cell(combnk(1:5, 5),2)']

Akzeptierte Antwort

Stephen23
Stephen23 am 24 Sep. 2019
Bearbeitet: Stephen23 am 24 Sep. 2019
>> vec = 1:5;
>> fun = @(n)num2cell(combnk(vec,n),2);
>> out = arrayfun(fun,1:numel(vec),'uniformoutput',0);
>> out = vertcat(out{:});
>> out{:}
ans =
1
ans =
2
ans =
3
ans =
4
ans =
5
ans =
1 2
ans =
1 3
ans =
1 4
ans =
1 5
ans =
2 3
ans =
2 4
ans =
2 5
ans =
3 4
ans =
3 5
ans =
4 5
ans =
1 2 3
ans =
1 2 4
ans =
1 2 5
ans =
1 3 4
ans =
1 3 5
ans =
1 4 5
ans =
2 3 4
ans =
2 3 5
ans =
2 4 5
ans =
3 4 5
ans =
1 2 3 4
ans =
1 2 3 5
ans =
1 2 4 5
ans =
1 3 4 5
ans =
2 3 4 5
ans =
1 2 3 4 5
>>

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by