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,
Info
This question is closed. Reopen it to edit or answer.
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
Geoff Hayes
on 27 May 2019
Debaditya Chakraborty
on 28 May 2019
Geoff Hayes
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?
JEET BANERJEE
on 18 Nov 2019
Edited: Walter Roberson
on 29 May 2020
I think the code (as shown below) generated by me is quite time saving and could solve it in two lines.
function [mmr,mmm] = minimax(M)
row_vector = abs(max(M,[],2)- min(M,[],2];
Overall = max(M,[],’all’)- min(M,[],’all’);
mmr = (row_vector)’;
mmm = Overall;
end
vignesh suresh
on 25 Feb 2020
function [mmr, mmm]= minimax(M)
mmr=max(M')-min(M');
mmm=[max(M(:))-min(M(:))];
Rik
on 15 Apr 2020
Hello everyone,
I am using Mathlab Online R2020a for this course.
I was able to pass this assignment with the following code:
function [mmr, mmm]= minimax(M)
mmr=max(M')-min(M');
mmm=[max(M(:))-min(M(:))];
Then calling it by the given [mmr, mmm] = minimax([1:4;5:8;9:12]) function.
BUT when i try to run this on Mathlab Online, i am getting the Unrecognized function or variable 'minimax' error. Does anyone know why? thanks.
P.s: i created a new script 'assignment' and wrote the code in there, in EDITOR then saved it. Then i tried to call the funtion by [mmr, mmm] = minimax([1:4;5:8;9:12]) in command window and it doesn't work. The other solutions, which are given here by other participants, don't work as well.
Regards
Sai Sandeep Kajuluri
on 12 May 2020
I got the same code of your's executed. kindly, name the file as minimax and try again
Zarin Rafa Shaitee
on 23 May 2020
Will you kindly describe why transpose of M is important? Thank you
Jessika Lisboa
on 29 May 2020
can you explain me the string? what does the string do and what it means for this function?
Walter Roberson
on 29 May 2020
I do not see anyone using string() for this topic ?
Sahil Deshpande
on 30 May 2020
function [mmr,mmm] = minimax(M)
mmr = abs(max(M.')-min(M.'));
mmm = max(max(M)) - min(min(M));
Shortest code I could write
abhishek kansal
on 7 Jun 2020
it works
Rik
on 11 Jun 2020
My reason for closing this question: people are just posting their answers to this homework question, without substantial discussion happening. Once there is an option to disallow new answers while allowing comments that option should be considered for this thread.
Matrika Shukla
on 18 Jul 2020
What does (M.') do exactly?
Walter Roberson
on 18 Jul 2020
M.' is transpose (not conjugate transpose, just plain transpose)
Answers (14)
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
Rik
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.
Rik
on 12 Jun 2019
In the answer why do we need to transpose the matrix M?
Rik
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).
Purushottam Shrestha
on 8 Jun 2020
We need to transpose because max(M.') gives a row vector of maximum elements of each row. I want you to try by giving command >>max(A.') Then you can see clearly.
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.
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
vinod suthar
on 9 Jun 2020
can please give the reason behind finding e as max(d) which is max(M).
Walter Roberson
on 9 Jun 2020
M is two dimensional. When you take max() of a two-dimensional matrix, then by default the maximum is taken for each column, so you would go from an m x n matrix to a 1 x n matrix of output. Then max() applied to that 1 x n matrix would take the maximum of those values, giving you a 1 x 1 result.
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
sandeep kumar singh
on 28 May 2020
yeah its working
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.
Shabarish Muralidharan
on 20 Jul 2020
Bro can anyone explain the logic behind this code
Jessica Avellaneda
on 22 Jul 2020
Can you explain please
Walter Roberson
on 22 Jul 2020
Are you asking about Nisheeth's code or about Sahil's code?
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
Debaditya Chakraborty
on 28 May 2019
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...
RAHUL KUMAR
on 8 May 2020
function [mmr mmm] = minimax(M);
mmr = (max(M,[],2) - min(M,[],2))';
mmm = max(M(:))-min(M(:));
end
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
1 Comment
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
5 Comments
Rohan Singla
on 17 Apr 2020
you can try this code as it genrated a successfull result .
RAHUL KUMAR
on 8 May 2020
function [mmr mmm] = minimax(M);
mmr = (max(M,[],2) - min(M,[],2))';
mmm = max(M(:))-min(M(:));
end
wikhyath Tirumala
on 12 May 2020
can i please know what does M' indicate???
Walter Roberson
on 12 May 2020
M' is conjugate transpose. Unless you are doing specialized linear algebra, it is recommended that you use .' instead of ' as .' is regular (non-conjugate) transpose.
Walter Roberson
on 12 May 2020
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
Rik
on 26 May 2020
Your code will only consider the first 3 rows. It will error for arrays that don't have 3 rows, and will return an incorrect result for arrays that have more than 3 rows.
You should read the documentation for max and min, and look through the other solutions on this thread for other possible strategies to solve this assignment.
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
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
saurav Tiwari
on 11 Jun 2020
0 votes
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
saurav Tiwari
on 11 Jun 2020
most easiest code of the world
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
Walter Roberson
on 23 Jul 2020
you have too many ( on two lines
Akinola Tomiwa
on 23 Jul 2020
Oh, alright, thanks I'll do well to write codes in smaller piece. Thanks a lot
Function [mmr, mmm] = minimax(x)
min_value = min(x,[], 2); % this takes the row minimum values of the supplied matrix
max_value = max(x, [],2); % this takes the row maximum values
mmr = (max_value-min_value)'; % the prime converts the colon to a row
mmm = (max(x(:)) - min(x(:)) ;
%x(:) converts the matrix to a single colon matrix so max(x(:)) returns a single value the maximum value
end
How is this ?
Walter Roberson
on 23 Jul 2020
mmm = (max(x(:)) - min(x(:)) ;
1 2 3 21 2 3 21
The number indicates the bracket nesting level in effect "after" the corresponding character. You can see that you have one open bracket in effect at the end of the line.
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!
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!