错误:返回列表中的元素编号2未定义

时间:2016-11-30 23:08:58

标签: matlab error-handling octave

以下函数正在生成错误。

function [retval] = select_red(train, test)
% shuffle test/data points
  shuffled_test = test(randperm(size(test, 1)), :);

  clubs     = shuffled_test(shuffled_test(:, 1) == 1, :);
  diamonds  = shuffled_test(shuffled_test(:, 1) == 2, :);
  hearts    = shuffled_test(shuffled_test(:, 1) == 3, :);
  spades    = shuffled_test(shuffled_test(:, 1) == 4, :);

  clubs = clubs(1: floor(size(clubs,1)/2),:);
  spades = spades(1: floor(size(spades,1)/2),:);

  shuffled_test = [clubs ; diamonds ; hearts ; spades];

  shuffled_test = shuffled_test(randperm(size(shuffled_test, 1)), :);

  ercf_indep = bayescls(train, shuffled_test, @pdfindep, 0.25 * ones(1,4), 0.1);
  ercf_dep = bayescls(train, shuffled_test, @pdfdep, 0.25 * ones(1,4), 0.1);
  ercf_parzen = bayescls(train, shuffled_test, @pdfparzen, 0.25 * ones(1,4), 0.1);

  retval = [ercf_indep   ercf_dep   ercf_parzen];
endfunction

用法

>> [errindep  errdep  errparzen] = select_red(train, test)
errindep =

    0.1089181   0.0029240   0.2309942

    error: element number 2 undefined in return list

你能说出为什么会产生错误信息吗?

1 个答案:

答案 0 :(得分:6)

你从糟糕的功能中得到了太多的要求。你想要三个输出,它只能输出一个(这是一个包含三个数字的向量)。

  • 如果您希望函数将三个数字作为单独的输出生成,请将其声明为

    [ercf_indep, ercf_dep, ercf_parzen] = select_red(train, test)
    

    并删除最后一行retval = ...

  • 如果您不想修改该功能,则需要使用一个输出调用它:

    out = select_red(train, test);
    

    然后将该输出解压缩为三个数字:

    ercf_indep_out = out(1)
    ercf_dep =  out(2);
    ercf_parzen = out(3);
    

    或者可以使用从单元格数组生成的逗号分隔列表来完成解包:

    out_cell = num2cell(out);
    [ercf_indep_out, ercf_dep, ercf_parzen] = out_cell{:}