Answer by Roger Stafford
on 23 Feb 2018

As James has stated, your question is not clear. I'm going to make a very wild guess as to your meaning. If it is wrong, as is likely, perhaps the method I show will give you some ideas of how you can achieve what you actually want.

Let your original 128x128x20x8 matrix be called A. Let the matrix you want to create be called B. I will suppose that your pairings in the 4th dimension of A are 1 and 2, 3 and 4, 5 and 6, 7 and 8. You then want to take all possible combinations of two pairs out of these four: 1,2,3,4 then 1,2,5,6 then 1,2,7,8, then 3,4,5,6 and so forth. This will give you a size of 4*6 = 24 at the fourth dimension.

C = nchoosek(1:2:7,2); % Choose 2 out of 4

n = size(C,1); % n = 4!/2!/2! = 6 in this case

B = repmat(zeros(size(A)),1,1,1,n/2); % B will have size 8*6/2=24 at 4th dimension

for ix = 1:n

B(:,:,:,4*ix-3:4*ix) = A(:,:,:,[C(ix,1),C(ix,1)+1,C(ix,2),C(ix,2)+1]);

end

Sign in to comment.

Answer by Amirah
on 26 Feb 2018

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## James Tursa (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/384287-how-to-generate-combination-through-specific-matrix-dimension#comment_538421

## Amirah (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/384287-how-to-generate-combination-through-specific-matrix-dimension#comment_539069

Sign in to comment.