【问题标题】:how to send multiple sets of different sizes to a matlab function?如何将多组不同大小的集合发送到 matlab 函数?
【发布时间】:2012-05-25 19:39:22
【问题描述】:

我有一个矩阵,其中每一行都有不同的大小,多余的地方用零填充。该矩阵取决于一些输入参数,因此具有动态而非固定的行数和大小。

for e.g.
1 2 4 5 0
1 3 0 0 0
1 2 3 4 5

如何创建 n 组不同大小(每行一组)并将其发送到另一个函数,例如购物车http://www.mathworks.com/matlabcentral/fileexchange/5475-cartprod-cartesian-product-of-multiple-sets?

【问题讨论】:

    标签: matlab parameters matrix arguments argument-passing


    【解决方案1】:

    如果您不希望cartprod 的输入向量中有任何零,您可以使用这个:

    CellArrayWithoutZeros = cellfun(@(x) x(find(x)), num2cell(Matrix, 2), 'UniformOutput', false);
    
    CartProdResultMatrix = cartprod(CellArrayWithoutZeros{:});
    

    编辑:如果您只想截断cartprod 的输入向量的前导零(在任何非零元素的右侧):

    CellArrayWithoutLeadingZeros = cellfun(@(x) x(1:find(x, 1, 'last')), num2cell(Matrix, 2), 'UniformOutput', false);
    
    CartProdResultMatrix = cartprod(CellArrayWithoutLeadingZeros{:});
    

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 2018-04-25
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多