필터 지우기
필터 지우기

Splitting an array in every possible combination

조회 수: 2 (최근 30일)
Selva Kumar
Selva Kumar 2022년 7월 14일
댓글: Selva Kumar 2022년 7월 16일
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
Jonas
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
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

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

채택된 답변

Voss
Voss 2022년 7월 15일
편집: Voss 2022년 7월 15일
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
Selva Kumar
Selva Kumar 2022년 7월 16일
This is exactly what I wished for !!!
Thanks a lot Voss ...
Thanks to Jonas, Dyuman Joshi, Steven Lord for your time and effort :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by