MATLAB Answers

Solving 2nd part of minimax question

15 ビュー (過去 30 日間)
Sarah Majin
Sarah Majin 2019 年 12 月 15 日
回答済み: Naveen Gehlot 2020 年 8 月 22 日
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 件のコメント
Madumathi Krishnan
Madumathi Krishnan 2020 年 5 月 11 日
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])

サインインしてコメントする。

採用された回答

John D'Errico
John D'Errico 2019 年 12 月 15 日
編集済み: John D'Errico 2019 年 12 月 15 日
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 件のコメント
Prathibha v
Prathibha v 2020 年 4 月 20 日
function [mmr, mmm]= minimax (A)
mmr = [max(A,[],2)- min(A,[],2)]';
mmm = max(A,[],'all')-min(A,[],'all');
end

サインインしてコメントする。

その他の回答 (2 件)

ERTIZA HOSSAIN SHOPNIL
ERTIZA HOSSAIN SHOPNIL 2020 年 5 月 6 日
function [mmr,mmm]= minimax(x)
A=x';
mmr=max(A)-min(A);
mmm=(max(A,[],'all')-min(A,[],'all'));
end

Naveen Gehlot
Naveen Gehlot 2020 年 8 月 22 日
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

Community Treasure Hunt

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

Start Hunting!

Translated by