Results 1 to 3 of 3

Math Help - Avoiding having ans returned (matlab)

  1. #1
    Junior Member
    Joined
    Oct 2009
    Posts
    31

    Avoiding having ans returned (matlab)

    Hi!

    I know a simple semicolon at the end of any command avoids output of the result of that command, but sometimes it shows up in any case. Any ideas?

    Example, my function below (it returns the positions of the maximum entry in a matrix, that is *not* on the diagonal):
    Code:
    function [r,s] = absmax(A)
    
    % do some error checking
    
    % Set diagonal to 0
    for k = 1:n
         A(k,k) = 0;
    end
    
    % Get the largest element from each column, store elements in m and indices in i
    [m, i] = max(abs(A));
    
    % Get the largest element from m, store it in t and its position in s
    [t, s] = max(m);
    
    % Pos r is the value of elementh s in i
    r = i(s);
    
    % Return r, s
    r
    s
    
    end
    Any ideas why the output has both r, s and an "ans" with the same value as r? Doesn't look clean

    Input on better ways of doing what I have done above is greatly appreciated!
    Last edited by Jodles; October 27th 2009 at 05:10 AM. Reason: wrong code
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    start by removing this from your code:
    Code:
    % Return r, s
    r
    s
    Then for the second part, if you dont specify an output variable name, the first variable of the function will be returned as "ans"

    so
    Code:
    absmax(rand(4))
    will return r as the variable "ans"

    Code:
    myVar_1 = absmax(rand(4))
    will return r as "myVar_1" and
    Code:
    [myVar_1 myVar_2] = absmax(rand(4))
    will return r as "myVar_1" and s as "myVar_2"

    Hope that helps,

    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Oct 2009
    Posts
    31
    Thank you, Elbarto!

    That makes perfect sense.

    All the best,
    Joachim
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. [SOLVED] Numerical Methods - Error propagation and avoiding cancellation
    Posted in the Advanced Math Topics Forum
    Replies: 1
    Last Post: October 20th 2011, 10:10 PM
  2. Avoiding Large Number of Terms in Equation Expansion
    Posted in the Advanced Math Topics Forum
    Replies: 0
    Last Post: April 28th 2011, 11:59 AM
  3. Avoiding having ans returned in a Matlab's function
    Posted in the Math Software Forum
    Replies: 3
    Last Post: July 13th 2010, 11:25 PM
  4. [SOLVED] Avoiding elliptic sine/cosine/etc
    Posted in the Advanced Math Topics Forum
    Replies: 3
    Last Post: May 25th 2010, 05:29 AM

Search Tags


/mathhelpforum @mathhelpforum