Splitting an array in every possible combination
显示 更早的评论
Hello,
I am attempting to split an array in all possible combination. I have illustrated it in the figure given below.

Depending on the number of rows, I wish to split the given array in all possible combinations.
One of the condition is Number of rows <= Number of elements in given array
After toiling for 3 days, I am posting this here.
Kindly guide me on how to accomplish this task.
Thanks for your time and valuable guidance.
12 个评论
Selva Kumar
2022-7-14
编辑:Selva Kumar
2022-7-14
maybe it helps you to see how such a matrix is generated. You can run this script multiple times to see different variants
you did still not tell us how many columns are allowed
numElements=randi([2 10]); % number of elements shall be random between 2 and 10
elements=1:numElements; % define elements
numOfRows=randi([2 numElements]); % random number of rows
elInRowNrX=zeros(numOfRows,1); % vector containing the number of elements per row
createdMatrix=[];
for rowNr=1:numOfRows
if rowNr<numOfRows
elInRowNrX(rowNr)=randi(numElements-sum(elInRowNrX)-(numOfRows-rowNr)); % we cannot enter more values than total number of values minus already given values minus 1 for each remaining row
createdMatrix(rowNr,1:elInRowNrX(rowNr))=elements(1:elInRowNrX(rowNr));
elements(1:elInRowNrX(rowNr))=[];
else
elInRowNrX(rowNr)=numElements-sum(elInRowNrX); % last row has to contian the remaining number of elements
createdMatrix(rowNr,1:elInRowNrX(rowNr))=elements;
end
end
disp(createdMatrix)
Steven Lord
2022-7-14
Those aren't "every possible combination". What about:
[x6 0 0 0 0 0;
x1 x2 x3 x4 x5 0]
What about:
[ 0 0 0 0 0 x1;
x2 x3 x4 x5 x6 0]
or:
[x1 0 0 0 0;
x2 x3 x4 x5 x6]
or
[x1 x2 x3;
x4 x5 x6]
What rules haven't you stated that disallow those options?
@Steven Lord to my understanding we have to fill the rows from the left and put in the elements one by one
your last case depends on a definition of number of columns, which was also not given. but the requested output can be generated by my script
numElements=6; % number of elements shall be random between 2 and 10
elements=1:numElements; % define elements
numOfRows=2; % random number of rows
elInRowNrX=zeros(numOfRows,1); % vector containing the number of elements per row
createdMatrix=[];
for rowNr=1:numOfRows
if rowNr<numOfRows
elInRowNrX(rowNr)=randi(numElements-sum(elInRowNrX)-(numOfRows-rowNr)); % we cannot enter more values than total number of values minus already given values minus 1 for each remaining row
createdMatrix(rowNr,1:elInRowNrX(rowNr))=elements(1:elInRowNrX(rowNr));
elements(1:elInRowNrX(rowNr))=[];
else
elInRowNrX(rowNr)=numElements-sum(elInRowNrX); % last row has to contian the remaining number of elements
createdMatrix(rowNr,1:elInRowNrX(rowNr))=elements;
end
end
disp(createdMatrix)
Selva Kumar
2022-7-14
编辑:Selva Kumar
2022-7-14
Dyuman Joshi
2022-7-14
Is the number of rows allowed only 2 and 3?
Selva Kumar
2022-7-14
Selva Kumar
2022-7-14
Dyuman Joshi
2022-7-14
This looks like a herculean task. I'll give it a try and update you.
Jonas
2022-7-14
@Selva Kumar, i know that it gives only one solution, i also wrote that it gives one at a time. but all of the solutions are at least valid. it should just be a help how the problem could finally be solved, that's why the code is a comment and not in the answer section
Jonas
2022-7-14
one 'solution' could be to run the script multiple times and collect unique solutions. but we still do not know restrictions on the number of columns
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!