Info
This question is closed. Reopen it to edit or answer.
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,
2 views (last 30 days)
Show older comments
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
15 Comments
Answers (15)
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])
5 Comments
Stephen23
on 17 Jul 2020
"We need to transpose because max(M.') gives a row vector of maximum elements of each row."
In some specific cases it will, but in general it does not.
"I want you to try by giving command >>max(A.') Then you can see clearly."
Okay, lets take a look:
>> A = [1;2;3]
A =
1
2
3
>> max(A.')
ans = 3
I can clearly see that this does NOT give the maximum of each row of A.
kannan vidyadhar
on 29 Apr 2020
Edited: kannan vidyadhar
on 29 Apr 2020
this is how i did
function [mmr,mmm]=minimax(A)
mmt=[max(A,[],2)-min(A,[],2)];
mmr=mmt'
mmm=max(A,[],"all")-min(A,[],"all")
3 Comments
Kailash Ramasubramaniam
on 9 May 2020
function [mmr,mmm]=minimax(r)
mmr= [max(r(1,[1:end]))- min(r(1,[1:end])),max(r(2,[1:end]))- min(r(2,[1:end])),...
max(r(3,[1:end]))- min(r(3,[1:end]))];
mmm=max(r(:))-min(r(:));
end
This the code which I wrote for this question. This works fine for matrices till 3 rows,after which it fails. I am new to matlab. Can someone help me to correct this code for random matrices please?
Arooba Ijaz
on 1 May 2020
function [mmr,mmm] =minimax (M)
%finding mmr
a=M'
b=max(a)
c=min(a)
mmr=b-c
%finding mmm
d=max(M)
e=max(d)
f=min(M)
g=min(f)
mmm=e-g
3 Comments
Rik
on 9 Jun 2020
This is done, because max only operates on a single dimension. Starting from R2018b you can specify a vector of dimensions, or use the 'all' keyword, see the documentation. In this answer they probably should have written max(M(:)) instead. I don't know who upvoted this function, as it is undocumented and takes a strange path to an answer.
Nisheeth Ranjan
on 28 May 2020
function [mmr,mmm]=minimax(A)
mmt=[max(A,[],2)-min(A,[],2)];
mmr=mmt'
mmm=max(max(A))-min(min(A))
This is the easiest code you cold ever find. Thank me later.
5 Comments
Geoff Hayes
on 27 May 2019
Edited: 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?
4 Comments
Sahil Deshpande
on 30 May 2020
function [mmr,mmm] = minimax(M)
mmr = abs(max(M.')-min(M.'));
mmm = max(max(M)) - min(min(M));
I did it this way
pradeep kumar
on 26 Feb 2020
function [mmr,mmm]=minimax(M)
mmr=abs(max(M')-min(M'));
mmm=(max(max(M'))-min(min(M')))
end
Rohan Singla
on 17 Apr 2020
function [mmr,mmm] = minimax(M)
a=M';
mmr=max(a,[],1)-min(a,[],1);
mmm= max(M(:)) - min(M(:));
end
AYUSH MISHRA
on 26 May 2020
function [mmr,mmm]=minimax(M)
mmr=max(M')-min(M');
mmm=max(max(M'))-min(min(M'));
end
% here M' is use because when we are using M than mmr generate column matrix
SOLUTION
[mmr, mmm] = minimax([1:4;5:8;9:12])
mmr =
3 3 3
mmm =
11
1 Comment
saurav Tiwari
on 11 Jun 2020
whatttt, it's so easy code omg and i make it very difficult. Same on me
Anurag Verma
on 26 May 2020
function [mmr,mmm]=minimax(M)
a = max(M(1,:))-min(M(1,:));
b = max(M(2,:))- min(M(2,:));
c = max(M(3,:))- min(M(3,:));
mmr = [a,b,c];
mmm = max(M(:))-min(M(:));
what's wrong with this code. can anyone explain please it gives an error with the random matrix question?
2 Comments
saurav Tiwari
on 11 Jun 2020
yaa, RIK is right. your code can only work for 3 rows matrix but random matrix contain a matrix of rows>1 . ok so, you should have to make a code that can work for any type of matrix
Md Naim
on 30 May 2020
function [mmr, mmm]= minimax(M)
mmr = max(M')-min(M')
mmm = max(max(M'))-min(min(M'))
end
0 Comments
ROHAN SUTRADHAR
on 6 Jun 2020
function [mmr,mmm] = minimax(A)
X = A';
mmr = max(X([1:end],[1:end]))- min(X([1:end],[1:end]));
mmm = max(X(:))-min(X(:));
end
0 Comments
saurav Tiwari
on 11 Jun 2020
function [a,b]=minimax(M)
[m,n]=size(M);
x=1:m;
a=max(M(x,:)')-min(M(x,:)');
v=M(:);
b=max(v)-min(v);
end
1 Comment
A.H.M.Shahidul Islam
on 21 Jul 2020
Edited: A.H.M.Shahidul Islam
on 21 Jul 2020
function [mmr,mmm]=minimax(M)
m=M';
mmr=abs(max(m)-min(m));
mmm=max(M(:))-min(M(:));
%works like a charm
1 Comment
Stephen23
on 21 Jul 2020
"works like a charm"
Does not work:
>> M = [1;2;4]
M =
1
2
4
>> minimax(M)
ans =
3
Akinola Tomiwa
on 23 Jul 2020
Function [mmr, mmm] = minmax(x)
mmr = (max(x, [], 2) - min(x, [], 2)';
%the prime converts it to a row matrix
mmm = (max(x(:)) - min(x(:));
end
4 Comments
youssef boudhaouia
on 24 Jul 2020
function [mmr,mmm]=minimax(M)
a=M';
ma=max(a);
mi=min(a);
mmr = ma - mi ;
mmm=max(max(M)) - min(min(M));
end
Here's my answer, as simple as possible and it works.
youssef boudhaouia
on 24 Jul 2020
function [mmr,mmm]=minimax(M)
a=M';
ma=max(a);
mi=min(a);
mmr = ma - mi ;
mmm=max(max(M)) - min(min(M));
end
here's my answer as simple as possible , it works!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!