简体   繁体   English

用于迭代选择矩阵列的 Octave 或 Matlab 代码

[英]Octave or Matlab code for iterative selection of the columns of a matrix

I have a matrix A composed of 4 columns C1, C2, C3 and C4.我有一个由 4 列 C1、C2、C3 和 C4 组成的矩阵 A。 I would like to have all the matrices (vectors) that can be generated from matrix A without repetitions of columns and where the order of the columns does not matter, ie matrix B = [C1 C2] = [C2 C1] .我想拥有可以从矩阵 A 生成的所有矩阵(向量)而无需重复列并且列的顺序无关紧要,即矩阵 B = [C1 C2] = [C2 C1] 。 More precisely I would like to obtain the following matrices / vectors: [C1];更准确地说,我想获得以下矩阵/向量:[C1]; [C2]; [C2]; [C3]; [C3]; [C4]; [C4]; [C1 C2]; [C1 C2]; [C1 C3]; [C1 C3]; [C1 C4]; [C1 C4]; [C2 C3]; [C2 C3]; [C2 C4 ]; [C2 C4]; [C3 C4]; [C3 C4]; [C1 C2 C3]; [C1 C2 C3]; [C1 C2 C4]; [C1 C2 C4]; [C1 C3 C4]; [C1 C3 C4]; [C2 C3 C4]; [C2 C3 C4]; [C1 C2 C3 C4]. [C1 C2 C3 C4]。 Can you please help me with an Octave code that can do this task.你能帮我写一个可以完成这个任务的 Octave 代码吗?

Thanks in advance提前致谢

George乔治

Here is MATLAB code to do it using logical indexing and arrayfun:这是使用逻辑索引和 arrayfun 执行此操作的 MATLAB 代码:

n = size(A,2); % number of columns
x = logical(dec2bin(1:2^n-1)-'0'); % logical array of all possible combinations
C = arrayfun(@(k)A(:,x(k,:)),1:size(x,1),'uni',false); % build the combinations

Result will be in the cell array C. Basically, each row of the dec2bin result is used for logical indexing into the columns of A. Note that this method is only practical for relatively small values of n.结果将在元胞数组 C 中。基本上,dec2bin 结果的每一行都用于对 A 的列进行逻辑索引。请注意,此方法仅适用于相对较小的 n 值。 For large n the memory requirements quickly become overwhelming.对于大 n,内存需求很快就会变得不堪重负。

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

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