[英]Matlab: How to find all possible combinations from 2 vectors?
V1
and V2
are vectors 1x20
and 1x10
respectively. V1
和V2
分别是向量1x20
和1x10
。
How to find all possible combinations of 15 elements from V1
and 3 elements from V2
? 如何找到
V1
中15个元素和V2
3个元素的所有可能组合?
The order of the selected elements is not important. 所选元素的顺序并不重要。
Here is one way: 这是一种方法:
% n-choose-k from each vector
A = nchoosek(1:20,15);
B = nchoosek(1:10,3);
% all combinations from the two sets
[X,Y] = ndgrid(1:size(A,1), 1:size(B,1));
C = [A(X(:),:) B(Y(:),:)];
The result contains the indices from the two vectors V1 and V2: 结果包含两个向量V1和V2的索引:
>> whos C
Name Size Bytes Class Attributes
C 1860480x18 267909120 double
If you want to directly apply it to the actual vectors, replace the first two lines with: 如果要直接将其应用于实际矢量,则将前两行替换为:
A = nchoosek(V1,15); % V1 is a 1x20 vector
B = nchoosek(V2,3); % V2 is a 1x10 vector
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.