필터 지우기
필터 지우기

How to generate all d combinations of elements of a vector of length M?

조회 수: 2 (최근 30일)
Mnr
Mnr 2016년 1월 26일
답변: Image Analyst 2016년 1월 26일
Hello all,
I have a vector of H length M; I would like to create a new matrix V with size dx(M^d), where each column of V contains each different d combinations of elements of M. For instance, if H=[1+i 2 1-i 1 0 -1-i 5 -1+i] and d=2, I would like to get a 2x(M^2)=2x64 matrix with the following columns: V1(first column of V)=[1+i 1+i].'; V2(second column of V)=[1+i 2].'; V3=[1+i 1-i].'; .... Can somebody please help me? Thank you!

답변 (1개)

Image Analyst
Image Analyst 2016년 1월 26일
I think this works, but I'm only getting 28 combinations if you have 8 elements and you take all combinations of 2 at a time:
H=[1+i,... % Element #1
2,... % Element #2
1-i,... % Element #3
1,... % Element #4
0,... % Element #5
-1-i,... % Element #6
5,... % Element #7
-1+i] % Element #8
M = length(H);
d = 2; % user specified
logicalIndexes = dec2bin(0:2^M-1) == '1'
% Find those rows where the number of elements is d:
rowsToExtract = sum(logicalIndexes, 2) == d
logicalIndexes = logicalIndexes(rowsToExtract, :) % Rows with only 2 elements
numCombinations = sum(rowsToExtract);
% Extract those rows from H
Hout = zeros(numCombinations, d)
% Load up Hout
for row = 1 : numCombinations
theseColumns = find(logicalIndexes(row,:))
for k = 1 : d
Hout(row, k) = H(theseColumns(k))
end
end
% Print to command window:
Hout
size(Hout)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by