matrix related matlab query

9 次查看(过去 30 天)
Siddharth Vidyarthi
编辑: DGM 2023-2-27
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)
A =
66 94 75 18
4 68 40 71
85 76 66 4
>> [x, y] = minimax(A)
x =
76 67 81
y =
90
  3 个评论
Sahil Deshpande
Sahil Deshpande 2020-5-30
编辑:Walter Roberson 2020-6-8
What do you guys think of this?
function [mmr,mmm] = minimax(M)
T = M.'; %Transposed matrix M
S = max(T) - min(T); %S will return a row vector of max - min values of each column of T, which is transpose of S.
%So S returns max - min of each row of M, which is required
mmr = abs(S); %gives the absolute value
mmm = max(max(M)) - min(min(M)); %max(M) and min (M) return a row vector, I used the function twice.
end

请先登录,再进行评论。

回答(9 个)

KETAN PATEL
KETAN PATEL 2019-6-11
function [mmr, mmm] = minimax(A);
B = A';
maxi= max(B);
mini = min(B);
mmr = max(B) - min(B);
mmm = max(maxi) - min(mini);
end
  7 个评论
KETAN PATEL
KETAN PATEL 2019-6-14
I have another problem and I just posted it in the community. It is titled as "if-statement with conditions". Could you please take a look if you have time? It's really easy but I don't where I am going wrong.
Ammara Haider
Ammara Haider 2019-12-17
thanks for your kind help

请先登录,再进行评论。


Saurabh Bhardwaj
Saurabh Bhardwaj 2020-6-8
function [a,b]=minimax(M)
A= min(M,[],2);
B= max(M,[],2);
a=(B-A)';
b=max(B)-min(A);
end
  1 个评论
DGM
DGM 2023-2-27
.' is the regular transpose
' is the complex conjugate transpose
It could use some commentary too. Otherwise, this is more thoughtful than most of the solutions on these threads.

请先登录,再进行评论。


RP
RP 2019-4-4
I saw this exercise on Coursera and seemed to have solved it, anyway when I ran the code it worked, but when I submit the answer and it is evaluated with random input, I get an error message every time. When I try to run it with the random numbers that were used for the evaluation, I get the correct results. Does anyone have the same problem? This is my code:
function [mmr, mmm] = minimax(M)
mmr = (max(M,[],2)-min(M,[],2))'
mmm = max(M(:))
end
  5 个评论
Crystal Judd Unson
Crystal Judd Unson 2021-4-25
编辑:Crystal Judd Unson 2021-4-25
Hi, I'm new to MATLAB so I'm a little confused on max(A,[],dim). How does this code instruct mmr to be a row vector and not a column vector? Why do I get an error message when
function [mmr, mmm] = test(M)
mmr = (max(M,[],0)-min(M,[],0))';
mmm = max(M(:))-min(M(:));
end
Thanks!
Steven Lord
Steven Lord 2021-4-25
x = magic(4);
max(x, [], 0)
Error using max
Dimension argument must be a positive integer scalar, a vector of unique positive integers, or 'all'.
Arrays in MATLAB do not have a dimension 0 so it does not make sense to ask for the maximum along that dimension.

请先登录,再进行评论。


RP
RP 2019-4-4
  4 个评论
sneha sharma
sneha sharma 2019-9-10
function [mmr,mmm]=minimax(A)
a=max(A(1,:))-min(A(1,:));
b=max(A(1,:))-min(A(1,:));
c=max(A(3,:))-min(A(3,:));
d=max(A(end,:))-min(A(end,:));
mmr=[a b c];
mmm=max(A(:))-min(A(:));
end
%this is my program it is not working for random matrices , can you define an error
VIJAY VIKAS MANGENA
What if the random matrix has more than 3 rows?
1)You have fixed the no.of outputs using this code.You get only 4 values ( if you meant ,b=max(A(2,:))-min(A(2,:));)
2)You have assumed that mmr can have only three outputs which is not always true..it depends on the matrix chosen and your code is supposed to work for any random matrix (the reason you got this error 'not working for random matrices'

请先登录,再进行评论。


AYUSH GURTU
AYUSH GURTU 2019-5-28
function [mmr, mmm] = minimax(M)
mmr = (max(M,[],2)-min(M,[],2))';
mmm = max(M(:))-min(M(:));
end
  3 个评论
RUSHI SHAH
RUSHI SHAH 2020-3-2
Can you please explain the syntax for mmr?
Ashitha Nair
Ashitha Nair 2020-6-15
M = max(A,[],dim) returns the maximum element along dimension dim. For example, if A is a matrix, then max(A,[],2) is a column vector containing the maximum value of each row.

请先登录,再进行评论。


Ashitha Nair
Ashitha Nair 2020-6-15
function [mmr,mmm]=minimax(M)
a=ceil(max(M.'));
b=ceil(min(M.'));
x=a-b;
mmr=x';
y=max(M(:));
z=min(M(:));
mmm=y-z;
end
This is how I've written it.
  2 个评论
Dorbala sankarshana
can you please explain this?
DGM
DGM 2023-2-27
Why would you take ceil()? That will give you the wrong result for non-integer inputs.

请先登录,再进行评论。


anuj petkar
anuj petkar 2020-9-13
function [mmr,mmm]=minimax(M)
A=(M(:,:))';
mmr=max(A(:,:))-min(A(:,:));
mmm=max(max(A))-min(min(A));
end

Amit Jain
Amit Jain 2020-10-24
function [mmr,mmm] = minimax(A)
T = A';
mmr = max(T)-min(T);
p= max(max(A(1:end,1:end)));
q = min(min(A(1:end,1:end)));
mmm= p-q;
end

ANDIE MEDDAUGH
ANDIE MEDDAUGH 2021-7-7
编辑:DGM 2023-2-27
Here's the code I used:
function [mmr, mmm] = minimax(M)
B = M';
maxie = max(B);
minnie = min(B);
mmr = abs(maxie - minnie)
mmm = abs(max(maxie) - min(minnie));
end
The max and min functions read columns, not rows. So the M' switches columns to rows, so that issue is resolved. Abs() is used to ensure absolute value and no negative numbers.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by