简体   繁体   English

如何仅合并向量-Matlab

[英]how do I combine only the vectors - Matlab

How do I combine all the vectors in small subsets of vectors in Matlab? 如何在Matlab中将所有向量合并为向量的小子集?

a= [5 6 7] b = [8 9 10] c=[11 12 13] d=[14 15 16] e=[17 18 19]

a combine with b and c: a与b和c结合:

Outcome: 结果:

M1= [ 5 6 7 8 9 10 11 12 13]

a with b and d: a与b和d:

M2 = [5 6 7 8 9 10 14 15 16]

and so on ..... 等等 .....

This answer covers the case for an arbitrary number of vectors . 这个答案涵盖了任意数量的向量的情况 The vectors are assumed to be row vectors of equal length. 假定矢量是等长的行矢量。

Let your example data be defined as 让您的示例数据定义为

a = [5 6 7]; b = [8 9 10]; c = [11 12 13]; d = [14 15 16]; e = [17 18 19];
vectors = {a, b, c, d, e}; %// cell array with any number of row vectors of equal size
n = 3; %// desired subset size

Then: generate all combinations of indices, use that to index into vectors , concatenate into one big row vector, and reshape that to obtain the desired result: 然后:生成索引的所有组合,使用该索引对vectors进行索引,将其连接为一个大行向量,并对它进行整形以获得所需的结果:

combs = nchoosek(1:numel(vectors), n);
result = reshape([vectors{combs.'}], numel(vectors{1})*n, []).';

This gives a matrix whose first row is your M1 , second row is M2 etc: 这给出了一个矩阵,其第一行是您的M1 ,第二行是M2等:

result =
     5     6     7     8     9    10    11    12    13
     5     6     7     8     9    10    14    15    16
     5     6     7     8     9    10    17    18    19
     5     6     7    11    12    13    14    15    16
     5     6     7    11    12    13    17    18    19
     5     6     7    14    15    16    17    18    19
     8     9    10    11    12    13    14    15    16
     8     9    10    11    12    13    17    18    19
     8     9    10    14    15    16    17    18    19
    11    12    13    14    15    16    17    18    19

You can use the cat function: 您可以使用cat函数:

res = cat(2,a,b,c);

or simply the [] syntax: 或者只是[]语法:

res = [a b c];

In both cases, res will contain [5 6 7 8 9 10 11 12 13] . 在这两种情况下, res都将包含[5 6 7 8 9 10 11 12 13]

Best, 最好,

Use nchoosek . 使用nchoosek

a = [5 6 7]; b = [8 9]; c = [11 12 13]; d = [14 15 16]; e = [17 18 19];
N = 3;
x = {a,b,c,d,e};
y = nchoosek(x,N);

And you have all the combination of your arrays taken N at a time in a cell array. 而且,您在一个单元阵列中一次获得N所有阵列组合。 Each row i of the cell x is a combination, so to have it back as a row vector just do 单元格x每一行i是一个组合,因此只要将其作为行向量返回即可

horzcat(y{i,:})

Or, if you want to get them all and put them in a cell array of size n_combs 或者,如果您想全部获取它们并将它们放在大小为n_combs的单元格数组中

n_combs = size(y,1);
out = cell(0,n_combs);
for i = 1 : n_combs
    out{i} = horzcat(1, y{i,:});
end

There is no constraint on the size of the arrays that you want to combine, eg, you can combine 对要合并的数组的大小没有限制,例如,可以合并

a = [5 7]; b = [8 9]; c = [11]; d = [20 14 15 16]; e = [17 18 19];

However, if you must put together all the combination in a matrix, then the arrays have to be of the same size. 但是,如果必须将所有组合放在一个矩阵中,则数组的大小必须相同。 In this case Luis Mendo's answer does the job. 在这种情况下,路易斯·门多的答案就可以了。

Finally, if repetitions are allowed use nmultichoosek instead of nchoosek . 最后,如果允许重复,请使用nmultichoosek而不是nchoosek

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM