Solving 2nd part of minimax question
3 views (last 30 days)
Show older comments
Sarah Majin
on 15 Dec 2019
Answered: Naveen Gehlot
on 22 Aug 2020
Good day, Please I've been stuck on this assignment for awhile now, i got the second part but the first part can't seem to make it work with random numbers. Had an eureka with 'mmm' and it was thrilling but my assignment is 'over' overdue and i really need a guide to solve 'mmr' properly. i'll greatly appreciate any help. Thank you
Here's what i've achieved so far.
mmr works but not with random numbers and it seems cumbersome.
function [mmr,mmm] = minimaxTrial(M)
A = ([M]);
A_max = max(A,[],'all');
A_min = min (A,[],'all');
mmm = (A_max - A_min);
[row,col] = size(A);
C_1 = (A(1,1:col));
C_2 = (A(2,1:col));
C_3 = (A(3,1:col));
C_4 = (A(n,1:col));
M1 = max(C_1) - min(C_1);
M2 = max(C_2) - min(C_2);
M3 = max(C_3) - min(C_3);
M4 = max(c_4) - min(C_4);
mmr =[M1, M2, M3, M4];
end
8 Comments
Turlough Hughes
on 15 Dec 2019
Madumathi Krishnan
on 11 May 2020
I got the output for that question
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
function [mmr,mmm]=minimax (M)
mmr=max(M,[],2)'-min(M,[],2)';
c=max(M,[],2);
d=min(M,[],2);
mmm=max(c)-min(d);
end
[mmr, mmm] = minimax([1:4;5:8;9:12])
Accepted Answer
John D'Errico
on 15 Dec 2019
Edited: John D'Errico
on 15 Dec 2019
I see you did answer the question in my comment.
"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"
This seems a little confusing as a question, because the difference between the max and min will ALWAYS be a positive number. So I'm not at all sure why it seemed important to indicate the absolute value. I guess that just means those writing homework assignments are human too?
Anyway, the global max minus the min is easy to write, as is the row-wise result.
function [mmr,mmm] = minimaxTrial(M)
mmm = max(A,[],'all') - min(A,[],'all');
mmr = max(A,[],2) - min(A,[],2);
end
One thing you don't want to do, is to extract each row of the matrix as you did.
C_1 = (A(1,1:col));
C_2 = (A(2,1:col));
C_3 = (A(3,1:col));
C_4 = (A(n,1:col));
That never works, because what happens if tomorrow your matrix has 3 or 5 rows? That little n at the end does not tell MATLAB to extend the pattern for n rows. (I suppose it should, but computers are not that smart. Well, not yet.)
Instead, you either needed to use the correct form for the max and min functions as I did, or you could have used a loop. Far better to write it in one line as I did though. using a loop, I might write it as:
function [mmr,mmm] = minimaxTrial(M)
mmm = max(A,[],'all') - min(A,[],'all');
[Ar,Ac] = size(A);
mmr = NaN(Ar,1);
for ir = 1:Ar
mmr(ir) = max(A(ir,:)) - min(A(ir,:));
end
end
See why this works for any number of rows.
Also see that I pre-allocated the vector mmr in the looped version. That is something you will need to do as your code becomes more sophisticated, because you don't want to dynamically adjust the size of vectors of arrays in a loop.
3 Comments
Prathibha v
on 20 Apr 2020
function [mmr, mmm]= minimax (A)
mmr = [max(A,[],2)- min(A,[],2)]';
mmm = max(A,[],'all')-min(A,[],'all');
end
More Answers (2)
ERTIZA HOSSAIN SHOPNIL
on 6 May 2020
function [mmr,mmm]= minimax(x)
A=x';
mmr=max(A)-min(A);
mmm=(max(A,[],'all')-min(A,[],'all'));
end
0 Comments
Naveen Gehlot
on 22 Aug 2020
function [mmr, mmm]= minimax (A)
mmr = [max(A,[],2)- min(A,[],2)]';
mmm = max(A,[],'all')-min(A,[],'all');
OUTPUT
mmr =
3 3 3
mmm =
11
0 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!