From a given vector create all combinations possible

Hello,
from a given vector i want to create all possible combinations.
I have the following code, a vector of [5 10 15] and I want to create the combinations of maxHiddenLayers numbers, it it has the value 2 i would get for example:
[5 5; 5 10; 5 15; 10 5; 10 10; 10 15; 15 5; 15 10; 15 15]
With the code i got i am not geting the [5 5; 10 10; 15 15], i can't have the same number in the columns and i wanted.
maxHiddenLayers = 2;
minNeurons = 5;
maxNeurons = 15;
rangeNeurons = minNeurons:5:maxNeurons;
nk = nchoosek(rangeNeurons,maxHiddenLayers)
p = zeros(0,maxHiddenLayers);
for i=1:size(nk,1),
pi = perms(nk(i,:));
p = unique([p; pi],'rows')
end

 채택된 답변

Stephen23
Stephen23 2019년 7월 26일
편집: Stephen23 2019년 7월 26일
>> V = [5,10,15];
>> [X,Y] = ndgrid(V);
>> M = [Y(:),X(:)]
M =
5 5
5 10
5 15
10 5
10 10
10 15
15 5
15 10
15 15

댓글 수: 4

Thanks.
It is possible to compute the M depending on the number of the variable maxHiddenLayers ? if i want 2 the outcome has 3 column, and if i want 3 the output has 3 columns?
Stephen23
Stephen23 2019년 7월 26일
편집: Stephen23 2019년 7월 26일
>> N = 3; % number of columns
>> V = [5,10,15];
>> C = cell(1,N);
>> [C{:}] = ndgrid(V);
>> C = cellfun(@(v)v(:),C,'uni',0);
>> M = [C{end:-1:1}]
M =
5 5 5
5 5 10
5 5 15
5 10 5
5 10 10
5 10 15
5 15 5
5 15 10
5 15 15
10 5 5
10 5 10
10 5 15
10 10 5
10 10 10
10 10 15
10 15 5
10 15 10
10 15 15
15 5 5
15 5 10
15 5 15
15 10 5
15 10 10
15 10 15
15 15 5
15 15 10
15 15 15
[combo_cell{1:maxHiddenLayers}] = ndgrid(V);
temp_cell = cellfun(@(M) M(:), combo_cell, 'uniform', 0);
M = horzcat(temp_cell{:});
Thanks for your input it is also corrected. like Stephen answer. I will accpect his answer because he was first. but your answer works as well! Thanks

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Profile and Improve Performance에 대해 자세히 알아보기

질문:

2019년 7월 26일

댓글:

2019년 7월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by