Write a MATLAB function zmaxofthetwox1x2 Which returns the m

Write a MATLAB function z=max_of_the_two(x1,x2)

Which returns the maximum of two non-negative numbers. If either of the inputs are negative numbers the function must return -1. DO not use the \"max\" function in matlab.

THEN:

write a matlab function z=compute-max(v) that returns the maximum of non negative numbers in the input vector v.if the input v contains one or more negative numbers the function must return -1.

thanks

thanks!!

Solution

--------------------------------------------------------------PROGRAM Function 2--------------------------------------------------

function z=compute_max(v)
if((sum(v<0))>0) % if any value of vector is negative than sum(v<0) is greater than 0
z=-1; % return -1 if sum(v<0) is greater than 0
return;
end
temp=0; % initialize temp=0
for i=1:numel(v) % loop for total elements
if(v(i)>temp) % check temp with v(i) for greater condition
temp=v(i); % assign v(i) into temp if v(i) is greater than temp
end
end
z=temp; % return temp value
end

Output:-

>> compute_max([ 1 2 10 4 8 0 3 5])

ans =

10

>> compute_max([ -1 -2 -3 -4 -8 -0 -3 -5])

ans =

-1

>> compute_max([ 1 2 3 4 8 0 3 -5])

ans =

-1

>>

--------------------------------------------------------------PROGRAM Function 1--------------------------------------------------

function z=max_of_the_two(x1,x2)
z=0; % initialize z as 0
if(x1<0 || x2<0) % Check if x1 or x2 any one is negative than return -1
z=-1;
return;
end
if(x1>x2) % if x1 is greater than x2 than z=x1 else z=x2
z=x1;   
else
z=x2;
end
end

Output:-

>> ans=max_of_the_two(3,-2)

ans =

-1

>> ans=max_of_the_two(3,2)

ans =

3

>> ans=max_of_the_two(2,3)

ans =

3

>>

Write a MATLAB function z=max_of_the_two(x1,x2) Which returns the maximum of two non-negative numbers. If either of the inputs are negative numbers the function
Write a MATLAB function z=max_of_the_two(x1,x2) Which returns the maximum of two non-negative numbers. If either of the inputs are negative numbers the function

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site