# matrix related matlab query

13 visualizzazioni (ultimi 30 giorni)
Siddharth Vidyarthi il 22 Mar 2019
Modificato: DGM il 27 Feb 2023
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)
A =
66 94 75 18
4 68 40 71
85 76 66 4
>> [x, y] = minimax(A)
x =
76 67 81
y =
90
##### 3 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
DGM il 26 Feb 2023
Modificato: DGM il 26 Feb 2023

Accedi per commentare.

### Risposte (9)

KETAN PATEL il 11 Giu 2019
function [mmr, mmm] = minimax(A);
B = A';
maxi= max(B);
mini = min(B);
mmr = max(B) - min(B);
mmm = max(maxi) - min(mini);
end
##### 7 CommentiMostra 6 commenti meno recentiNascondi 6 commenti meno recenti
Ammara Haider il 17 Dic 2019

Accedi per commentare.

Saurabh Bhardwaj il 8 Giu 2020
function [a,b]=minimax(M)
A= min(M,[],2);
B= max(M,[],2);
a=(B-A)';
b=max(B)-min(A);
end
##### 1 CommentoMostra NessunoNascondi Nessuno
DGM il 27 Feb 2023
.' is the regular transpose
' is the complex conjugate transpose
It could use some commentary too. Otherwise, this is more thoughtful than most of the solutions on these threads.

Accedi per commentare.

RP il 4 Apr 2019
I saw this exercise on Coursera and seemed to have solved it, anyway when I ran the code it worked, but when I submit the answer and it is evaluated with random input, I get an error message every time. When I try to run it with the random numbers that were used for the evaluation, I get the correct results. Does anyone have the same problem? This is my code:
function [mmr, mmm] = minimax(M)
mmr = (max(M,[],2)-min(M,[],2))'
mmm = max(M(:))
end
##### 5 CommentiMostra 4 commenti meno recentiNascondi 4 commenti meno recenti
Steven Lord il 25 Apr 2021
x = magic(4);
max(x, [], 0)
Error using max
Dimension argument must be a positive integer scalar, a vector of unique positive integers, or 'all'.
Arrays in MATLAB do not have a dimension 0 so it does not make sense to ask for the maximum along that dimension.

Accedi per commentare.

RP il 4 Apr 2019 ##### 4 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
VIJAY VIKAS MANGENA il 13 Ago 2020
What if the random matrix has more than 3 rows?
1)You have fixed the no.of outputs using this code.You get only 4 values ( if you meant ,b=max(A(2,:))-min(A(2,:));)
2)You have assumed that mmr can have only three outputs which is not always true..it depends on the matrix chosen and your code is supposed to work for any random matrix (the reason you got this error 'not working for random matrices'

Accedi per commentare.

AYUSH GURTU il 28 Mag 2019
function [mmr, mmm] = minimax(M)
mmr = (max(M,[],2)-min(M,[],2))';
mmm = max(M(:))-min(M(:));
end
##### 3 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Ashitha Nair il 15 Giu 2020
M = max(A,[],dim) returns the maximum element along dimension dim. For example, if A is a matrix, then max(A,[],2) is a column vector containing the maximum value of each row.

Accedi per commentare.

Ashitha Nair il 15 Giu 2020
function [mmr,mmm]=minimax(M)
a=ceil(max(M.'));
b=ceil(min(M.'));
x=a-b;
mmr=x';
y=max(M(:));
z=min(M(:));
mmm=y-z;
end
This is how I've written it.
##### 2 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
DGM il 27 Feb 2023
Why would you take ceil()? That will give you the wrong result for non-integer inputs.

Accedi per commentare.

anuj petkar il 13 Set 2020
function [mmr,mmm]=minimax(M)
A=(M(:,:))';
mmr=max(A(:,:))-min(A(:,:));
mmm=max(max(A))-min(min(A));
end
##### 1 CommentoMostra NessunoNascondi Nessuno
DGM il 27 Feb 2023
A(:,:)
is the same as
A

Accedi per commentare.

Amit Jain il 24 Ott 2020
function [mmr,mmm] = minimax(A)
T = A';
mmr = max(T)-min(T);
p= max(max(A(1:end,1:end)));
q = min(min(A(1:end,1:end)));
mmm= p-q;
end
##### 1 CommentoMostra NessunoNascondi Nessuno
DGM il 27 Feb 2023
A(1:end,1:end)
is the same as
A

Accedi per commentare.

ANDIE MEDDAUGH il 7 Lug 2021
Modificato: DGM il 27 Feb 2023
Here's the code I used:
function [mmr, mmm] = minimax(M)
B = M';
maxie = max(B);
minnie = min(B);
mmr = abs(maxie - minnie)
mmm = abs(max(maxie) - min(minnie));
end
The max and min functions read columns, not rows. So the M' switches columns to rows, so that issue is resolved. Abs() is used to ensure absolute value and no negative numbers.
##### 0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Time Series Collections in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!