Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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,

3 ビュー (過去 30 日間)
Debaditya Chakraborty
Debaditya Chakraborty 2019 年 5 月 27 日
閉鎖済み: Rik 2020 年 7 月 24 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 7 月 18 日
M.' is transpose (not conjugate transpose, just plain transpose)

回答 (14 件)

mayank ghugretkar
mayank ghugretkar 2019 年 6 月 5 日
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 件のコメント
Purushottam Shrestha
Purushottam Shrestha 2020 年 6 月 8 日
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
Stephen23 2020 年 7 月 17 日
"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
Arooba Ijaz 2020 年 5 月 1 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 6 月 9 日
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
Rik 2020 年 6 月 9 日
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
Nisheeth Ranjan 2020 年 5 月 28 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 7 月 22 日
Are you asking about Nisheeth's code or about Sahil's code?

Geoff Hayes
Geoff Hayes 2019 年 5 月 27 日
編集済み: Geoff Hayes 2019 年 5 月 27 日
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?
Take a look at max and min and in particular the "dimension to operate along" parameter and see how that can be used to find the minimum and maximum value in each row (as opposed to in each column).
  4 件のコメント
RAHUL KUMAR
RAHUL KUMAR 2020 年 5 月 8 日
function [mmr mmm] = minimax(M);
mmr = (max(M,[],2) - min(M,[],2))';
mmm = max(M(:))-min(M(:));
end
Sahil Deshpande
Sahil Deshpande 2020 年 5 月 30 日
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
pradeep kumar 2020 年 2 月 26 日
function [mmr,mmm]=minimax(M)
mmr=abs(max(M')-min(M'));
mmm=(max(max(M'))-min(min(M')))
end
  1 件のコメント
Rik
Rik 2020 年 2 月 26 日
編集済み: Stephen23 2020 年 7 月 17 日
Why would you use the transpose if you can also simply use the third input argument for min?
Also, max(max(M')) is equivalent to max(max(M)) and max(M(:)) (and also to max(M,[],'all'), so you could even use that).

Rohan Singla
Rohan Singla 2020 年 4 月 17 日
function [mmr,mmm] = minimax(M)
a=M';
mmr=max(a,[],1)-min(a,[],1);
mmm= max(M(:)) - min(M(:));
end
  5 件のコメント
Walter Roberson
Walter Roberson 2020 年 5 月 12 日
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.

AYUSH MISHRA
AYUSH MISHRA 2020 年 5 月 26 日
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 件のコメント
saurav Tiwari
saurav Tiwari 2020 年 6 月 11 日
whatttt, it's so easy code omg and i make it very difficult. Same on me

Anurag Verma
Anurag Verma 2020 年 5 月 26 日
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 件のコメント
Rik
Rik 2020 年 5 月 26 日
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
saurav Tiwari 2020 年 6 月 11 日
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
Md Naim 2020 年 5 月 30 日
function [mmr, mmm]= minimax(M)
mmr = max(M')-min(M')
mmm = max(max(M'))-min(min(M'))
end

ROHAN SUTRADHAR
ROHAN SUTRADHAR 2020 年 6 月 6 日
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
saurav Tiwari 2020 年 6 月 11 日
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

A.H.M.Shahidul Islam
A.H.M.Shahidul Islam 2020 年 7 月 21 日
編集済み: A.H.M.Shahidul Islam 2020 年 7 月 21 日
function [mmr,mmm]=minimax(M)
m=M';
mmr=abs(max(m)-min(m));
mmm=max(M(:))-min(M(:));
%works like a charm
  1 件のコメント
Stephen23
Stephen23 2020 年 7 月 21 日
"works like a charm"
Does not work:
>> M = [1;2;4]
M =
1
2
4
>> minimax(M)
ans =
3

Akinola Tomiwa
Akinola Tomiwa 2020 年 7 月 23 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 7 月 23 日
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
youssef boudhaouia 2020 年 7 月 24 日
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
youssef boudhaouia 2020 年 7 月 24 日
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!

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by