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 Commenti

Jonas
Jonas il 14 Lug 2022
Modificato: Jonas il 14 Lug 2022
are there restriction on the number of columns? or is the number given?
rows with 0 only are not allowed?
Selva Kumar
Selva Kumar il 14 Lug 2022
Modificato: Selva Kumar il 14 Lug 2022
Thank you for the swift response Jonas.
Yes, rows with zeros are not allowed.
Each row must have atleast 1 non zero element.
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)
1 0 0 0 0 2 3 4 5 6 7 8 9 10 0
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)
1 2 3 4 5 6
Selva Kumar
Selva Kumar il 14 Lug 2022
Modificato: Selva Kumar il 14 Lug 2022
Jonas,
Your script gives one of the correct combination while I require all possible combinations.
For example, if number of elements in array is 6 and number of rows is 2, then there will be a total of 5 possible combinations. I have illustrated all the possible 5 combinations in the figure attached.
If number of elements in array is 6 and number of rows is 3, then there will be a total of 10 possible combinations. I have illustrated all the possible 5 combinations in the figure attached.
In order to make my question clearer, I am listing few points
  1. Presence of zeroes at the end is allowed.
  2. 1<=Number of columns<=Number of elements in the array.
  3. Each row shall atleast one element.
Is the number of rows allowed only 2 and 3?
Both the combinations given below are same.
[ 0 0 0 0 0 x1;
x2 x3 x4 x5 x6 0]
and
[x1 0 0 0 0;
x2 x3 x4 x5 x6]
I have stated the number of rows as an example.
I wish to change the number of rows as per my requirement.
The number of rows shall be less than equal to number of elements in the array
This looks like a herculean task. I'll give it a try and update you.
@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
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

Accedi per commentare.

 Risposta accettata

Voss
Voss il 15 Lug 2022
Modificato: Voss il 15 Lug 2022
I believe this will do the required "splitting" of the array:
format compact
x = 1:6;
disp(split_x(x,1));
1 2 3 4 5 6
disp(split_x(x,2));
(:,:,1) = 1 0 0 0 0 2 3 4 5 6 (:,:,2) = 1 2 0 0 0 3 4 5 6 0 (:,:,3) = 1 2 3 0 0 4 5 6 0 0 (:,:,4) = 1 2 3 4 0 5 6 0 0 0 (:,:,5) = 1 2 3 4 5 6 0 0 0 0
disp(split_x(x,3)); % scroll down to see them all
(:,:,1) = 1 0 0 0 2 0 0 0 3 4 5 6 (:,:,2) = 1 0 0 0 2 3 0 0 4 5 6 0 (:,:,3) = 1 0 0 0 2 3 4 0 5 6 0 0 (:,:,4) = 1 0 0 0 2 3 4 5 6 0 0 0 (:,:,5) = 1 2 0 0 3 0 0 0 4 5 6 0 (:,:,6) = 1 2 0 0 3 4 0 0 5 6 0 0 (:,:,7) = 1 2 0 0 3 4 5 0 6 0 0 0 (:,:,8) = 1 2 3 0 4 0 0 0 5 6 0 0 (:,:,9) = 1 2 3 0 4 5 0 0 6 0 0 0 (:,:,10) = 1 2 3 4 5 0 0 0 6 0 0 0
disp(split_x(x,4)); % scroll down to see them all
(:,:,1) = 1 0 0 2 0 0 3 0 0 4 5 6 (:,:,2) = 1 0 0 2 0 0 3 4 0 5 6 0 (:,:,3) = 1 0 0 2 0 0 3 4 5 6 0 0 (:,:,4) = 1 0 0 2 3 0 4 0 0 5 6 0 (:,:,5) = 1 0 0 2 3 0 4 5 0 6 0 0 (:,:,6) = 1 0 0 2 3 4 5 0 0 6 0 0 (:,:,7) = 1 2 0 3 0 0 4 0 0 5 6 0 (:,:,8) = 1 2 0 3 0 0 4 5 0 6 0 0 (:,:,9) = 1 2 0 3 4 0 5 0 0 6 0 0 (:,:,10) = 1 2 3 4 0 0 5 0 0 6 0 0
disp(split_x(x,5)); % scroll down to see them all
(:,:,1) = 1 0 2 0 3 0 4 0 5 6 (:,:,2) = 1 0 2 0 3 0 4 5 6 0 (:,:,3) = 1 0 2 0 3 4 5 0 6 0 (:,:,4) = 1 0 2 3 4 0 5 0 6 0 (:,:,5) = 1 2 3 0 4 0 5 0 6 0
disp(split_x(x,6));
1 2 3 4 5 6
function x_split = split_x(x,n_rows)
N = numel(x);
n_cols = N-n_rows+1;
split_idx = nchoosek(1:N-1,n_rows-1);
n_combos = size(split_idx,1);
split_idx = [zeros(n_combos,1) split_idx N*ones(n_combos,1)];
n_split = diff(split_idx,1,2);
x_split = zeros(n_rows,n_cols,n_combos);
for ii = 1:n_combos
for jj = 1:n_rows
x_split(jj,1:n_split(ii,jj),ii) = x(split_idx(ii,jj)+(1:n_split(ii,jj)));
end
end
end

1 Commento

This is exactly what I wished for !!!
Thanks a lot Voss ...
Thanks to Jonas, Dyuman Joshi, Steven Lord for your time and effort :)

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by