How generate combination list of 4 independent vectors?

조회 수: 5 (최근 30일)
Dominik Vana
Dominik Vana 2020년 2월 29일
편집: Stephen23 2025년 7월 29일
Hello,
I have a question. I am trying to generate list of 4 vectors. For example v1=[ 1; 2; 3, 4]; v2=[ 5; 6; 7]; v3=[ 8; 9]; v4=[ 10] will generate matrix with 24 rows (number of possible combinations) and with 4 columns.
I tried to use 4 for-loops, but when input vectors have more elements (200 elements for each vector), the algorithm is starting to be slow. I would like to ask you for a help. Is there any other possible way or a matlab function to create this thing?
Thank You, Dominik.
M1_idx=[1; 2; 3; 4]; M2_idx=[5; 6; 7]; M3_idx=[8; 9]; M4_idx=[10];
[r1,c1] = size(M1_idx);
[r2,c2] = size(M2_idx);
[r3,c3] = size(M3_idx);
[r4,c4] = size(M4_idx);
K_mat = zeros(r1*r2*r3*r4,c1+c2+c3+c4);
for i=1:r1
for j=1:r2
for k=1:r3
for l=1:r4
K_mat((i-1)*r2*r3*r4 + (j-1)*r3*r4 + (k-1)*r4 + l,:) = [M1_idx(i) M2_idx(j) M3_idx(k) M4_idx(l)];
end
end
end
end
  댓글 수: 2
Stephen23
Stephen23 2020년 3월 1일
편집: Stephen23 2020년 3월 1일
"...when input vectors have more elements (200 elements for each vector), the algorithm is starting to be slow."
This is not really a surprise, have you calculated how much memory that would require?:
>> bits = 200*200*200*200 * 64
bits = 102400000000
>> num2sip(bits/8) % bytes
ans = 12.8 G
Does your computer have enough memory to hold four of arrays of that size? And then one array of size
>> num2sip(4*bits/8) % bytes
ans = 51.2 G
so you would need more than 100 GB of memory in total. I don't know of many desktop computers with that much memory. What calculations do you imagine doing on arrays of that size?
You could save memory by using an integer class, e.g. uint8 only requires 13 GB or so.
Dominik Vana
Dominik Vana 2020년 3월 1일
I know, I am trying to search solution in 3D space based on values from 4 sensors. It is part of my Master's thesis.

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

채택된 답변

Stephen23
Stephen23 2020년 3월 1일
편집: Stephen23 2025년 7월 29일
A simple solution for a fixed number of vectors:
v1 = [1;2;3;4];
v2 = [5;6;7];
v3 = [8;9];
v4 = [10];
[x1,x2,x3,x4] = ndgrid(v1,v2,v3,v4);
M = [x1(:),x2(:),x3(:),x4(:)]
M = 24×4
1 5 8 10 2 5 8 10 3 5 8 10 4 5 8 10 1 6 8 10 2 6 8 10 3 6 8 10 4 6 8 10 1 7 8 10 2 7 8 10 3 7 8 10 4 7 8 10 1 5 9 10 2 5 9 10 3 5 9 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A general efficient solution which expands to any number of vectors (which should be stored in one cell array, as using numbered variables invariably leads to inefficient code):
C = {v1,v2,v3,v4}; % the vectors should be stored in one cell array.
N = numel(C);
[C{:}] = ndgrid(C{:});
M = reshape(cat(N+1,C{:}),[],N) % thanks to Guillaume.
M = 24×4
1 5 8 10 2 5 8 10 3 5 8 10 4 5 8 10 1 6 8 10 2 6 8 10 3 6 8 10 4 6 8 10 1 7 8 10 2 7 8 10 3 7 8 10 4 7 8 10 1 5 9 10 2 5 9 10 3 5 9 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  댓글 수: 2
Guillaume
Guillaume 2020년 3월 1일
The cell2mat(cellfun(..)) could be replaced by:
M = reshape(cat(numel(C)+1, C{:}), [], numel(C))
which I suspect would be faster.
Stephen23
Stephen23 2025년 7월 27일
@Guillaume: agreed, that is a very neat approach. I modifed my answer to suit.

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

추가 답변 (1개)

Bálint Udvardy
Bálint Udvardy 2020년 2월 29일
Try to use the 'combvec' function. It generates all possible conbinations of the given vectors. For your case it can be called as:
K_mat=combvec(v1',v2',v3',v4')';
Or for the latter:
K_mat=combvec(M1_idx',M2_idx',M3_idx',M4_idx')';
The transpositions were added due to the fact that the function requires a column vectors; the last transposition assures only provides better readibility of the result.
Hope I could help. ;)
  댓글 수: 1
Dominik Vana
Dominik Vana 2020년 3월 1일
Thank you Balint, I tried it, but your suggestion is slower than my solution for larger vectors.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by