如何禁用Octave函数内的输出变量

时间:2015-04-07 17:09:46

标签: matlab octave

我为Octave编写了自己的函数,但不幸的是,除了最终结果值之外,变量"结果"在每次更改时写入控制台,这是一种不受欢迎的行为。

>> a1 = [160 60]
a1 =

   160    60

>> entr = my_entropy({a1}, false)
result =  0.84535
entr =  0.84535

应该是

>> a1 = [160 60]
a1 =

   160    60

>> entr = my_entropy({a1}, false)
entr =  0.84535

我不知道〜并且它不起作用,至少在我尝试的时候。 代码如下:

# The main difference between MATLAB bundled entropy function
# and this custom function is that they use a transformation to uint8
# and the bundled entropy() function is used mostly for signal processing
# while I simply use a straightforward solution usefull e.g. for learning trees

function f = my_entropy(data, weighted)
  # function accepts only cell arrays;
  # weighted tells whether return one weighed average entropy
  # or return a vector of entropies per bucket
  # moreover, I find vectors as the only representation of "buckets"
  # in other words, vector = bucket (leaf of decision tree)
  if nargin < 2
    weighted = true;
  end;

  rows = @(x) size(x,1);
  cols = @(x) size(x,2);

  if weighted
    result = 0;
  else
    result = [];
  end;

  for r = 1:rows(data)

    for c = 1:cols(data) # in most cases this will be 1:1

      omega = sum(data{r,c});
      epsilon = 0;

      for b = 1:cols(data{r,c})
        epsilon = epsilon + ( (data{r,c}(b) / omega) * (log2(data{r,c}(b) / omega)) );
      end;

      if (-epsilon == 0) entropy = 0; else entropy = -epsilon; end;

      if weighted
        result = result + entropy
      else
        result = [result entropy]
      end;

    end;

  end;

  f = result;

end;

# test cases

cell1 = { [16];[16];[2 2 2 2 2 2 2 2];[12];[16] }
cell2 = { [16],[12];[16],[2];[2 2 2 2 2 2 2 2],[8 8];[12],[8 8];[16],[8 8] }
cell3 = { [16],[3 3];[16],[2];[2 2 2 2 2 2 2 2],[2 2];[12],[2];[16],[2] }

# end

2 个答案:

答案 0 :(得分:3)

在代码中;result = result + entropy之后添加result = [result entropy],或者在您不希望在屏幕上打印的任何作业之后添加char


如果由于某种原因你无法修改该功能,可以使用evalc来防止不需要的输出(至少在Matlab中)。请注意,此情况下的输出是以T = evalc(expression)形式获得的:

  

eval(expression)T相同,除了通常写入命令窗口的任何内容(错误消息除外)都会被捕获并返回到字符数组T中(行\n中的eval字符分隔。

与任何entr = evalc('my_entropy({a1}, false)'); 变体一样,如果可能,应避免使用此方法:

{{1}}

答案 1 :(得分:3)

在您的代码中,您应该以分号;结束第39行和第41行。

以分号结束的行未在stdout中显示。