## Write a function called minimax that takes M, a matrix input argument and returns mmr, a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row. As a second output argument called mmm,

### Debaditya Chakraborty (view profile)

on 27 May 2019
Latest activity Commented on by Rik

### Rik (view profile)

on 12 Jun 2019
Write a function called minimax that takes M, a matrix input argument and returns mmr, a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row. As a second output argument called mmm, it provides the difference between the maximum and minimum element in the entire matrix. See the code below for an example:
>> A = randi(100,3,4) %EXAMPLE
A =
66 94 75 18
4 68 40 71
85 76 66 4
>> [x, y] = minimax(A)
x =
76 67 81
y =
90
%end example
%calling code: [mmr, mmm] = minimax([1:4;5:8;9:12])
Is my logic correct?
my approach
function [a,b]= minimax(M)
m=M([1:end,0);
a= [abs(max(M(m))-min(M(m)))];
b= max(M(:)) - min(M(:));
end

Geoff Hayes

on 27 May 2019

### Debaditya Chakraborty (view profile)

on 28 May 2019
How can make this work for any given matrix??
especially function a
Geoff Hayes

### Geoff Hayes (view profile)

on 28 May 2019
The code that you have posted will work for any given matrix though you probably want to remove the m as it is unclear why it is needed. Have you looked at the documentation to see how to use min and max to get the minimum or maximum across each row?

R2018b

### mayank ghugretkar (view profile)

Answer by mayank ghugretkar

### mayank ghugretkar (view profile)

on 5 Jun 2019

here's my function....
went a little descriptive for good understanding to readers.
function [a,b]=minimax(M)
row_max=max(M');
overall_max=max(row_max);
row_min=min(M');
overall_min=min(row_min);
a=row_max - row_min;
b=overall_max-overall_min;
Code to call your function
[mmr, mmm] = minimax([1:4;5:8;9:12])

Rik

### Rik (view profile)

on 5 Jun 2019
In general completely working solutions to homework questions are discouraged. The point of homework is to learn, not to outsource work to strangers on the internet. Sometimes helping someone understand a problem and help them solving it on their own can take more time, but it is a lot more satifying than solving someone else's homework.
A broader view on the topic of homework can be found here.
Rik

### Rik (view profile)

on 12 Jun 2019
Comment by Jagmeet Singh mistakenly posted as answer:
In the answer why do we need to transpose the matrix M?
Rik

### Rik (view profile)

on 12 Jun 2019
Because the min function operates only in one direction. If you want it to operate along a different direction, you can either transpose the matrix (as Mayank has done), or use the third input, as described in the documentation (which I would suggest).

### Geoff Hayes (view profile)

Answer by Geoff Hayes

### Geoff Hayes (view profile)

on 27 May 2019
Edited by Geoff Hayes

### Geoff Hayes (view profile)

on 27 May 2019

Is my logic correct?
I'm not clear on why you need the m. In fact, doesn't the line of code
m=M([1:end,0);
fail since there is no closing square bracket? What is the intent of this line?
Take a look at max and min and in particular the "dimension to operate along" parameter and see how that can be used to find the minimum and maximum value in each row (as opposed to in each column).

### Debaditya Chakraborty (view profile)

on 28 May 2019
how can I assign each row to this function one after the other?
Geoff Hayes

### Geoff Hayes (view profile)

on 28 May 2019
how can I assign each row to this function one after the other?
I don't understand your question. Why do you need to assign each row to this function? If you do
>> min(A, [], 2)
then that will return a vector/array with the minimum value in each row of A...