Matlab 在单元格结构中查找值的最大组合?
如果我有以下单元格:Matlab 在单元格结构中查找值的最大组合?,matlab,cell,max,combinations,Matlab,Cell,Max,Combinations,如果我有以下单元格: 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 2或3 2 3中的最大组合,因为两者在a{4}的3/4列中都有值3(最大值),并且a{5}max
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 2
或3 2 3
中的最大组合,因为两者在a{4}
的3/4列中都有值3
(最大值),并且a{5}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的每列中包含最大的元素。我相信您需要以下内容:
[~, 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
你想在这里做什么?您的预期输出是什么?@wakjah我更新了问题谢谢您解释我的错误,我不知道max()
会这样做。您能告诉我代码是如何工作的吗??我很想学习这个方法。