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,
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
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])
My answer to this:
function [mmr,mmm] = minimax(M)
mmr = abs(max(M.')-min(M.'));
mmm = max(max(M)) - min(min(M));
This is shortest code I could write. What do you guys think of this?
댓글 수: 14
Fails when there are multiple rows but each row contains only one element.
This was already discussed here: https://www.mathworks.com/matlabcentral/answers/464240-write-a-function-called-minimax-that-takes-m-a-matrix-input-argument-and-returns-mmr-a-row-vector#comment_713890
Simply put: your assumption that min and max always operate over the row dimension is incorrect: it actually depends on the size of the input array. The robust solution is thus not to rely on behavior which changes with array size, but to simply specify the dimension argument.
Sahil Deshpande
2020년 5월 30일
Ritika Mehra
2020년 8월 24일
What is the meaning of max(M.'). I didn't get that command.
Emirhan Bilgiç
2020년 10월 27일
Nice job but abs is arbitrary, you already subtract min from max. So conclusion will be always positive.
Keerthi M S
2020년 12월 2일
this code works thank you so much.
Chandan Kumar
2021년 2월 23일
편집: Chandan Kumar
2021년 2월 23일
this code dont work for me so i did some changes and now its like this and now its good for me.
function [mmr,mmm] = minimax(M)
mmr = abs(max(M.')-min(M.'))
mmm = max(M,[],'all')-min(M,[],'all')
end
Hritik Chowdhary
2021년 9월 1일
why are we taking transpose of M here while calculating mmr?
henok gashaw
2021년 11월 26일
because max(M) gives the maximum value of the column vector.
Rik
2021년 11월 26일
You can also use the optional third input to change the default direction that max operates on: max(M,[],2)
Stephen23
2021년 11월 26일
"because max(M) gives the maximum value of the column vector."
It takes the maximum along the first non-scalar dimension.
Which is the cause of the bug in many of the answers on this page.
M KK
2022년 7월 6일
Hello ,
why do .' in max(M.') does why you have used it ?
khaula
2022년 11월 7일
not working still, can any one code it rightly please??
See also:
답변 (17개)
Prasad Reddy
2020년 5월 30일
function [mmr,mmm] = minimax(M)
a=max(M');
b=min(M');
mmr=a-b;
c=max(a);
d=min(b);
mmm=c-d;
end
% This is what i came up with. Please give a upthumb if it works.
댓글 수: 3
Incorrect output:
>> M = [1;2;3];
>> minimax(M)
ans = 2
The correct output would be:
[0;0;0]
Alexandar
2022년 6월 24일
How come you put a single apostrophe for this: M'. I am having trouble understanding that portion since I am new to coding.
Rik
2022년 6월 24일
The apostrophe is the operator to determine the conjugate. In the case of non-complex numbers that means swapping the rows with columns.
Rushi Auti
2020년 7월 31일
function [mmr,mmm] = minimax(M)
a = max(M,[],2);
b = min(M,[],2);
c= a-b;
d = c';
mmr = c'
e = max(M,[],'all');
f = min(M,[],'all');
mmm = e-f
댓글 수: 12
Rushi Auti
2020년 7월 31일
I wrote this code.
Aniket Bordekar
2020년 7월 31일
Can you pelese explain why is c' taken?
Rushi Auti
2020년 8월 2일
편집: DGM
2023년 3월 29일
bcz we want the difference between the maximum and minimum numbers.
actually we can reduce it like this. This will also run better.
function [mmr,mmm] = minimax(M)
a = max(M,[],2);
b = min(M,[],2);
mmr = (a-b)';
e = max(M,[],'all');
f = min(M,[],'all');
mmm = e-f;
end
Aniket Bordekar
2020년 8월 2일
Yes, but actually my question was why that transpose is needed?
ali yasser
2020년 8월 7일
편집: ali yasser
2020년 8월 7일
yes ,cuz in example he want answer as vector not 3 by 1 matrix i guess
Nur Ameera Nabila Abdul Rahim
2020년 8월 16일
편집: DGM
2023년 3월 29일
may i know what is the 2 at the a = max(M,[],2) means ?
may i know also what is the [] in the code means ?
Rik
2020년 8월 17일
@Nur, did you read the documentation for the max function? It will explain this syntax to you.
Samson Ihuoma
2020년 8월 17일
Pls guys I've been using the free 1month trial for matlab and it has currently expired... I need a license no, can someone help me out?
Rik
2020년 8월 17일
If you are a student: ask your school/university IT for a license or buy the discounted student license. If you are not a student: ask your company's IT for a license.
Asking for this on a public forum may not be the smartest move. I doubt anyone here will even help you find illegal options.
Wenceslao Jr Sevilla
2020년 11월 7일
@rushi I got my student account with the help of coursera
Renz Reven Mariveles
2022년 6월 11일
Thank youu. omg I have been searching for so longg. I HAVE FINALLY FIND THE ANSWER.
Shanu
2022년 6월 24일
Thanku so much😊
Ahmed Salmi
2020년 7월 17일
function [mmr,mmm]=minimax(m)
mmr=max(m')-min(m');
mmm=max(m,[],'all')-min(m,[],'all');
end
or
function [mmr,mmm]=minimax(m)
a=max(m');
b=min(m');
mmr=a-b;
c=max(m,[],'all');
d=min(m,[],'all');
mmm=c-d;
end
댓글 수: 1
Stephen23
2020년 7월 17일
Incorrect output:
>> M = [1;2;3]
M =
1
2
3
>> minimax(M)
ans = 2
Harry Virani
2020년 8월 12일
function [mmr, mmm] = minimax(input)
matrix = [input];
maxr = max(matrix.');
minr = min(matrix.');
mmr = maxr - minr;
maxm = max(maxr);
minm = min(minr);
mmm = maxm - minm;
end
댓글 수: 1
Stephen23
2020년 8월 17일
Fails for any matrix with only one column:
>> minimax([1;2;3])
ans = 2
durgesh patel
2021년 1월 4일
function [mmr , mmm] = minimax(M)
mmr = max(M') - min(M');
mmm = max(M,[],'all')- min(M,[],'all');
end
댓글 수: 1
Fails for any matrix with only one column:
minimax([1;2;3])
Shamith Raj Shetty
2021년 1월 4일
편집: DGM
2023년 3월 29일
function [mmr,mmm] = minimax(M)
N = M';
mmr = max(N)-min(N);
mmm = max(max(N))-min(min(N));
end
댓글 수: 1
Your function fails for column vectors.
M = [1;2;3];
minimax(M) % ans = [0,0,0]
M=[1:4;5:8;9:12];
minimax(M) % ans = [3,3,3]
Also, what is the point of posting this answer? What does it teach? Why should it not be deleted?
Francisco Moto
2021년 2월 6일
0 개 추천

댓글 수: 2
Francisco Moto
2021년 2월 6일
My function works but it failed the random question . Need help
Stephen23
2021년 2월 6일
@Francisco Moto: your function does not do what your assignment requires. In particular:
- Your function accepts one input. It then ignores this input completely.
- You have hard-coded values for one specific matrix. The assignment requests a general solution.
Most of the operations in your function are not used for anything.
Balakrishna Peram
2021년 6월 8일
편집: Stephen23
2021년 6월 8일
on a General sense this should be the answer
function [mmr,mmm] = minimax(M)
mmr=abs(max(M,[],2)-min(M,[],2))
mmm=max(M,[],'all')-min(M,[],'all')
end
댓글 수: 1
Fazal Hussain
2022년 1월 19일
편집: DGM
2023년 3월 29일
There is some mistake in second line but now it will give you output okay.
thanks
function [mmr,mmm] = minimax(M)
mmr=[abs(max(M,[],2)-min(M,[],2))]';
mmm=max(M,[],'all')-min(M,[],'all');
end
Chappa Likhith
2021년 6월 25일
In editor window:
function [mmr,mmm]=minimax(M)
mmr=difference(M') %M' is a tranpose of M. If you want to know why this.. go to COMPUTER PROGRAMMING WITH MATLAB book of author J. MICHAEL FITZPATRICK AND ÁKOS LÉDECZI... go to page 90 tabel 2.7
mmm=difference(M(:));
function a=difference(v)
a=max(v)-min(v);
In comand window:
>>>[mmr, mmm] = minimax([1:4;5:8;9:12])
% you can write any other matrix too
댓글 수: 3
Walter Roberson
2021년 6월 25일
This code is incorrect for the case where M has complex values. The meaning of minimum and maximum of complex numbers is not clear, but this code will get it wrong.
If it does not have complex values, then it is better style to use M.' rather than M'
Chappa Likhith
2021년 6월 25일
May be you are correct. I'm not that much familiar with matlab and I don't know for what M.' is used for... This is the question in coursera assignment of vanderbilt university. This question appears after completion of few topics where the topic of M.' is not covered.... My answer is for them who are facing the same situation like me. Because I too didn't got the answer for a long time and I saw your solution(I think so) I didn't understand what's going on in your code. I hope you understand my situation...
I have not posted a solution for this, as it is a homework question, and I avoid posting complete answers to homework questions.
The difference between M' and M.' is that M.' is plain transpose, but M' is conjugate transpose.
M = [1+2i 2-3i 4]
M'
M.'
Notice that in the M' that the signs of the complex part have changed but in the M.' version they do not change. You can see from the final entry that the result is the same for values that have no complex part.
As a matter of style, I recommend that you always use .' unless you specifically need conjugate transpose: using .' will save people having to think a lot about your code to figure out whether you should have used .' instead of '
Vetrimurasu Baskaran
2022년 6월 6일
편집: DGM
2023년 2월 26일
function [mmr,mmm] = minimax(M)
r = size(M);
val = r(1);
mmr = inf;
for i = 1:val
mmr(i) = max(M(i,1:end)) - min(M(i,1:end));
end
A = M(:);
mmm = max(A)-min(A);
end
댓글 수: 0
Adwaith G
2022년 6월 27일
I am new to Matlab , so i am explaining what i learned here.
Initially i solved it by using the code
function [mmr,mmm] = minimax(M)
mmr = max(M.')-min(M.');
mmm = max(M(:))-min(M(:));
# Both M' and M.' gives the transpose of a matrix. However, M' gives the conjugate transpose. So, I suggest that u only use M.'
However, this code fails if the matrix has only 1 column. So, i used the code
function [mmr,mmm] = minimax(M)
mmk = max(M,[],2)-min(M,[],2);
mmr = mmk.';
mmm = max(M(:))-min(M(:));
#max(M,[],2) computes the max value of each row and returns a column vector and in order to get a row vector, we take the transpose.
댓글 수: 0
function [mmr,mmm]=minimax(M)
mmr=[abs([max(M.')-min(M.')])]
mmm=abs([(max(M(:))-(min(M(:)))])
end
댓글 수: 1
Walter Roberson
2022년 7월 22일
What purpose do those [ ] serve in the body of the code?
mmm=abs([(max(M(:))-(min(M(:)))])
123 4 5 43 4 5 6 54321
You have one more open bracket than you have close brackets
Arah Cristal
2022년 10월 11일
편집: DGM
2023년 3월 29일
function [mmr,mmm] = minimax (m)
mmr = abs(max(m.')-min(m.'))
mmm = (max(m,[],'all')-min(m,[],'all'))
댓글 수: 1
Fails for any matrix with only one column:
minimax([1;2;3])
function [mmr,mmm] = minimax (m)
mmr = abs(max(m.')-min(m.'));
mmm = (max(m,[],'all')-min(m,[],'all'));
end
Muhammad Faizan Ahmed
2022년 12월 18일
이동: DGM
2023년 3월 29일
function [mmr, mmm] = minimax(M)
mmr = max(M')-min(M');
mmm = max(max(M)) - min(min(M));
댓글 수: 0
Hassan
2023년 3월 29일
function [mmr,mmm]=minimax(M)
mmr=[abs(max(M(1:end,:).')-min(M(1:end,:).'))];
mmm=max(M(:))-min(M(:));
end
댓글 수: 2
Fails for any matrix with only one column:
minimax([1;2;3])
function [mmr,mmm]=minimax(M)
mmr=[abs(max(M(1:end,:).')-min(M(1:end,:).'))];
mmm=max(M(:))-min(M(:));
end
- There's no point in doing abs(max(X)-min(X)). Think about why.
- There's no point in doing X(1:end,:). Think about why.
- There's no point in doing [X]. Think about why.
- Why reshape/transpose the array multiple times instead of just once?
How do so many people keep writing these same nonsense things unless:
- they're just building collages of code based on other bad code
- people somehow gravitate to these superfluous decorations when they want to make their code appear superficially unique for some bizarre purpose
This isn't where you turn in your assignment. There's little merit in posting something unless it correctly answers the question or provides the reader with new information. It should then stand to reason that there's little merit in repeating prior examples which have been demonstrated to be incorrect.
If you're going to post an answer in a thread full of junk answers, try to break that trend. Post an answer which is tested and documented. Explain why your answer is different than others (both strengths and weaknesses are important to know). Since you can run your code in the editor, you have the opportunity to demonstrate that it does what you say it does.
function [mmr,mmm] = minimax(M);
mmr=(max(M,[],2)-min(M,[],2))';
mmm=max(M,[],"all")-min(M,[],"all");
end
% this is what i did
댓글 수: 0
function [x, y] = minimax(M)
x = (max(M,[],2) - min(M,[],2))';
y = max(M,[], "all")- min(M,[], "all");
end
댓글 수: 1
DGM
2024년 2월 5일
While this is correct, it's not really any different than the answer above it. The only difference is the change in output variable names, which are (I assume) dictated by the assignment. If the grader actually requires the outputs to be mmr, mmm respectively, then this would be a problem. Fixing the variable names would make this a duplicate answer.
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!