Splitting an array in every possible combination
Mostra commenti meno recenti
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 Commenti
Selva Kumar
il 14 Lug 2022
Modificato: Selva Kumar
il 14 Lug 2022
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
il 14 Lug 2022
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
il 14 Lug 2022
Modificato: Selva Kumar
il 14 Lug 2022
Dyuman Joshi
il 14 Lug 2022
Is the number of rows allowed only 2 and 3?
Selva Kumar
il 14 Lug 2022
Selva Kumar
il 14 Lug 2022
Dyuman Joshi
il 14 Lug 2022
This looks like a herculean task. I'll give it a try and update you.
Jonas
il 14 Lug 2022
@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
il 14 Lug 2022
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
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Creating and Concatenating Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!