Matlab中矩阵行值的笛卡尔积

Cartesian product of row values of a marix in Matlab

类似,我在 Matlab 中有一个实数值矩阵(包括 NaNs)A,维度为 mxn。我想构造一个矩阵 B 按行列出 As 列中包含的值的非唯一笛卡尔积的每个元素,这些列不是 NaN。为了更清楚地考虑以下示例。

示例:

  %m=3;
  %n=3;

  A=[2.1 0 NaN;
     69 NaN 1;
     NaN 32.1 NaN];

  %Hence, the Cartesian product {2.1,0}x{69,1}x{32.1} is 
  %{(2.1,69,32.1),(2.1,1,32.1),(0,69,32.1),(0,1,32.1)} 

  %I construct B by disposing row-wise each 3-tuple in the Cartesian product


 B=[2.1 69 32.1;
    2.1 1 32.1;
    0 69 32.1;
    0 1 32.1];

我想出了一个使用细胞的解决方案:

function B = q48444528(A)
if nargin < 1
A = [2.1   0   NaN;
      69  NaN   1 ;
     NaN  32.1 NaN];
end
% Converting to a cell array of rows:
C = num2cell(A,2);
% Getting rid of NaN values:
C = cellfun(@(x)x(~isnan(x)),C,'UniformOutput',false);
% Finding combinations:
B = combvec(C{:}).';

输出:

B =

    2.1000   69.0000   32.1000
         0   69.0000   32.1000
    2.1000    1.0000   32.1000
         0    1.0000   32.1000