Obtain every possible combination in array

I have 3, 100 x 1 vectors that I am combining to make a m x 4 matrix. To obtain 4 columns in the desired output, two elements from the first vector are selected as the first two elements in a row, and then one element from the second vector as the third element, and one element from the third vector as the fourth element to complete the m x 4. I would like to get every possible row combination possible in one large matrix. I started with a huge nested for loop and quickly got lost and assume there must be a better way. Any help is appreciated.

댓글 수: 4

dpb
dpb 2017년 3월 30일
For what definition of "possible" in "every possible row combination" are we talking about here...the row indices or the unique values in each column or the unique pairs of values or...? Also, are elements 1,2 the same as 2,1 that is are we also talking permutations besides combinations?
Hi Tyler, As you said, you are talking one large matrix. Assuming that the choices from the first vector are two independent choices, then you will have a matrix with 100x100x100x100 = 1e8 rows and 4 columns. You may want to rethink this.
Also, if your vectors contain the usual Matlab real numbers at 8 bytes apiece to store, that's 3.2 gigabytes of required memory.
Tyler Murray
Tyler Murray 2017년 3월 30일
By possible I mean row indices. Order does not matter I am just looking for the total number of combinations. Thus no permutations necessary. Thanks for your reply, I'd appreciate any help.
Kirby Fears
Kirby Fears 2017년 3월 30일
편집: Kirby Fears 2017년 3월 30일
The combinations total to (100)*99/2*100*100 = 49,500,000 rows. It's probably worth rethinking this approach. I will post some code below anyway...

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

답변 (1개)

Kirby Fears
Kirby Fears 2017년 3월 30일
편집: Kirby Fears 2017년 5월 8일

0 개 추천

Here's a brute force generation of all combinations. Memory usage is more of a bottleneck than processing time, so I wrote it with for-loops.
Note that storing all combinations may not be the best approach to address your underlying use case.
Set sz = 100 to match your vector size; beware of 50 million rows.
% Setting up dummy data
sz = 10; % 4500 rows
a = (1:sz)';
b = a + sz;
c = b + sz;
res = NaN(sz^3*(sz-1)/2,4);
% Filling array with selections
iCounter = 0;
for ai = 1:(numel(a)-1),
for aj = (ai+1):numel(a),
for bi = 1:sz,
for ci = 1:sz,
iCounter = iCounter + 1;
res(iCounter,:) = [a(ai) a(aj) b(bi) c(ci)];
end
end
end
end

댓글 수: 2

If each element of vector with size (1,14) takes one value from this vector [0 0.625 1.25 2.5] . How can I obtain all possible combinations of this vector ?

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2017년 3월 30일

댓글:

2017년 5월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by