Asked by Debaditya Chakraborty
on 27 May 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

Answer by mayank ghugretkar
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
on 5 Jun 2019

Rik
on 12 Jun 2019

In the answer why do we need to transpose the matrix M?

Rik
on 12 Jun 2019

Sign in to comment.

Answer by Geoff Hayes
on 27 May 2019

Edited by Geoff Hayes
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?

Debaditya Chakraborty
on 28 May 2019

how can I assign each row to this function one after the other?

Geoff Hayes
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...

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Geoff Hayes (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/464240-write-a-function-called-minimax-that-takes-m-a-matrix-input-argument-and-returns-mmr-a-row-vector#comment_709094

## Debaditya Chakraborty (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/464240-write-a-function-called-minimax-that-takes-m-a-matrix-input-argument-and-returns-mmr-a-row-vector#comment_709281

## Geoff Hayes (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/464240-write-a-function-called-minimax-that-takes-m-a-matrix-input-argument-and-returns-mmr-a-row-vector#comment_709382

## JEET BANERJEE (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/464240-write-a-function-called-minimax-that-takes-m-a-matrix-input-argument-and-returns-mmr-a-row-vector#comment_768610

Sign in to comment.