Results 1 to 5 of 5

Math Help - Find maximum value

  1. #1
    Newbie
    Joined
    Apr 2010
    Posts
    23

    Find maximum value

    Code:
    function max()
    
    A = [ 5 4 7 2 6 3 1 ];
    
    max=0;
    
    
    for j = 1:7,
      if(  A(j) > max ) max = A(j);
      end %first end
    % line A
    end  %second end
    
    max
    When i insert the max word at line A i get it printed all the time the loop is iterated, even though it's outside the for loop.

    Why is that?
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Newbie
    Joined
    Apr 2010
    Posts
    23
    The above is Matlab code
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by AUCC View Post
    Code:
    function max()
    
    A = [ 5 4 7 2 6 3 1 ];
    
    max=0;
    
    
    for j = 1:7,
      if(  A(j) > max ) max = A(j);
      end %first end
    % line A
    end  %second end
    
    max
    When i insert the max word at line A i get it printed all the time the loop is iterated, even though it's outside the for loop.

    Why is that?
    It is not outside the for-loop, that ends at the second end, the first end terminates the if-construct.

    CB
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Apr 2010
    Posts
    23
    Should there be an end statement to terminate the function ?
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by AUCC View Post
    Should there be an end statement to terminate the function ?
    It won't do any harm, but is redundant.

    A single function in an .m file does not require and end (and as far as I recall a function definition in a script file is terminated by the eof marker or the next function statement - the same goes for functions and sub-functions in the same file)

    Good practice would say put the end statement in to terminate the function, Matlab will catch up eventually and insist on it.

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Find the maximum
    Posted in the Number Theory Forum
    Replies: 0
    Last Post: March 31st 2011, 06:00 AM
  2. Find the maximum value...
    Posted in the Number Theory Forum
    Replies: 1
    Last Post: September 4th 2010, 08:12 AM
  3. Find the maximum
    Posted in the Algebra Forum
    Replies: 4
    Last Post: April 5th 2008, 12:20 PM
  4. Find the maximum
    Posted in the Trigonometry Forum
    Replies: 12
    Last Post: January 23rd 2008, 12:08 PM
  5. find the maximum
    Posted in the Algebra Forum
    Replies: 5
    Last Post: January 9th 2008, 05:21 AM

Search Tags


/mathhelpforum @mathhelpforum