【问题标题】:Cartesian product of row values of a marix in MatlabMatlab中矩阵行值的笛卡尔积
【发布时间】:2018-07-04 19:11:41
【问题描述】:

this question 类似,我在Matlab 中有一个维度为mxn 的实数值矩阵(包括NaNs)A。我想构造一个矩阵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];

【问题讨论】:

  • 对于任何mn
  • 是的,它应该适用于任何二维矩阵。

标签: matlab nan cartesian-product


【解决方案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

【讨论】:

  • @Codevan 需要详细说明吗?
  • @Codevan which 其他矩阵
猜你喜欢
  • 2012-04-07
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 2023-03-29
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多