# Math Help - Find maximum value

1. ## 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?

2. The above is Matlab code

3. Originally Posted by AUCC
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

4. Should there be an end statement to terminate the function ?

5. Originally Posted by AUCC
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