Mathbeginner
Mathbeginner

Reputation: 201

Octave keeps giving results from function although not asked

I created a function in Octave for which I, at this moment, only want one of the possible outputs displayed. The code:

function [pi, time, numiter] = PageRank(pi0,H,v,n,alpha,epsilon);
  rowsumvector=ones(1,n)*H';
  nonzerorows=find(rowsumvector);
  zerorows=setdiff(1:n,nonzerorows); l=length(zerorows);
  a=sparse(zerorows,ones(l,1),ones(l,1),n,1);

  k=0;
  residual=1;
  pi=pi0;
  tic;

  while (residual >= epsilon)
      prevpi=pi;
      k=k+1;
      pi=alpha*pi*H + (alpha*(pi*a)+1-alpha)*v;
      residual = norm(pi-prevpi,1);
  end

  pi;  
  numiter=k
  time=toc;

endfunction

Now I only want numiter returned, but it keeps giving me back pi as well, no matter whether I delete pi;, or not.

It returns it in the following format:

>> PageRank(pi0,H,v,length(H),0.9,epsilon)
numiter =  32
ans =

   0.026867   0.157753   0.026867   0.133573   0.315385 

To me it seems strange that the pi is not given with its variable, but merely as an ans.

Any suggestions?

Upvotes: 0

Views: 130

Answers (1)

Cris Luengo
Cris Luengo

Reputation: 60514

I know the Octave documentation for this is not very extensive, but perhaps it gives enough hints to understand that how you think about output variables is wrong.

The call

PageRank(pi0,H,v,length(H),0.9,epsilon)

returns a single output argument, it is equivalent to

ans = PageRank(pi0,H,v,length(H),0.9,epsilon)

ans is always the implied output argument if none is explicitly given. ans will be assigned the value of pi, the first output argument of your function. The variable pi (nor time, nor numiter) in your workspace will be modified or assigned to. These are the names of local variables inside your function.

To obtain other output variables, do this:

[out1,out2,out3] = PageRank(pi0,H,v,length(H),0.9,epsilon)

Now, the variable out1 will be assigned the value that pi had inside your function. out2 will contain the value of time, and out3 the value of numiter,

If you don't want the first two output arguments, and only want the third one, do this:

[~,~,out3] = PageRank(pi0,H,v,length(H),0.9,epsilon)

The ~ indicates to Octave that you want to ignore that output argument.

Upvotes: 3

Related Questions