【问题标题】:Find the maximum combination of values in a Cell structure?找到 Cell 结构中值的最大组合?
【发布时间】:2013-03-26 17:50:56
【问题描述】:

如果我有以下单元格:

a = cell(5,1);
a{1} = [1 3 1 0];
a{2} = [3 1 3 3];
a{3} = [3 2 3 2];
a{4} = [3 3 3 2];
a{5} = [3 2 3 3];

输入max(cell2mat(a)) 会得到ans = 3 3 3 3

但这没有意义,因为3 3 3 3 甚至不存在于那个细胞结构中!!到底是怎么回事 ?以及如何找到细胞结构的最大组合?

注意:我指的是3 3 3 23 2 3 3 中的最大组合——因为两者在a{4} 的3/4 列中都有值3(最大值)和a{5}

【问题讨论】:

  • 你想在这里做什么?您的预期输出是什么?
  • @wakjah 我更新了问题

标签: matlab cell max combinations


【解决方案1】:

我相信您想要以下内容:

[~, maxInd] = max(sum(cell2mat(a), 2));
a{maxInd}

ans =

 3     3     3     2

如果您希望 所有 行与具有最大值的行具有相同的总值,那么您可以这样做:

% Take the sum along the rows of a
summedMat = sum(cell2mat(a), 2); 
% Find the value from the summed rows that is the highest
maxVal = max(summedMat);         
% Find any other rows that also have this total
maxInd = summedMat == maxVal;
% Get them rows!
a{maxInd}

ans =

 3     3     3     2


ans =

 3     2     3     3

【讨论】:

  • 你能告诉我代码是如何工作的吗?很想学习这个方法。
【解决方案2】:

max(A) 给出返回 A 每一列的最大值。

那么,如果

A = cell2mat(a)
A =

   1   3   1   0
   3   1   3   3
   3   2   3   2
   3   3   3   2

max(A)是一个向量,包含A的每一列中最大的元素。

【讨论】:

  • 感谢您解释我的错误,不知道 max() 会这样做。
猜你喜欢
  • 2021-03-15
  • 1970-01-01
  • 2016-01-31
  • 2018-01-08
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 2018-07-04
相关资源
最近更新 更多