Matlab-创建细胞混淆矩阵

时间:2016-09-24 15:12:47

标签: arrays matlab matrix cell-array confusion-matrix

我有以下单元格矩阵,它将用作混淆矩阵:

confusion=cell(25,25);

然后,我有另外两个单元格数组,每行包含预测标签(数组输出)和另一个包含真实标签的单元格矩阵(数组groundtruth)。

whos output 

 Name             Size               Bytes  Class    Attributes

 output      702250x1             80943902  cell               

whos groundtruth                       
  Name                  Size               Bytes  Class    Attributes

  groundtruth      702250x1             84270000  cell               

然后,我创建了以下脚本来创建混淆矩阵

function confusion=write_confusion_matrix(predict, groundtruth)

   confusion=cell(25,25);

   for i=1:size(predict,1)
        confusion{groundtruth{i},predict{i}}=confusion{groundtruth{i}, predict{i}}+1;
   end
end

但是当我在matlab中运行它时出现以下错误:

Index exceeds matrix dimensions.

Error in write_confusion_matrix (line 4)
confusion{groundtruth{i},predict{i}}=confusion{groundtruth{i}, predict{i}}+1;

我很想打印输出和groundtruth的值来看看发生了什么

 output{1}                                                                                                              

 ans =

 2

 groundtruth{1}                                                                                                         

 ans =

 1

所以,价值似乎没有错,所以这里有什么问题?是代码中混淆矩阵的索引权吗?

1 个答案:

答案 0 :(得分:1)

错误发生在million循环中。在这种情况下,检查循环的第一次迭代是不够的。 for表示Index exceeds matrix dimensions范围内的i 1:size(output,1)groundtruth{i}大于25。

您可以找出哪一个元素至少有一个大于该范围的元素:

output{i}

或者你可以数数:

% 0 means no, there is none above 25. 1 means yes, there exists at least one:
hasoutlier = any(cellfun(@(x) x > 25, groundtruth)) % similar for 'output'

也许你也想找到这些元素:

outliercount = sum(cellfun(@(x) x > 25, groundtruth))

顺便说一下,我想知道为什么你在这种情况下使用单元阵列?为什么不是数字数组?