matrix related matlab query

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 件のコメント

Stephan
Stephan 2019 年 3 月 22 日
This appears to be homework - to be exactly: your homework.
Please provide your attempts so far and ask a specific question to the problem you have by trying on your own.
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 日

2 投票

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 月 11 日
編集済み: KETAN PATEL 2019 年 6 月 11 日
The is issue with this problem is that the matrix given in the question is a 3X4 matrix. Hence, the max(of that matrix) will be a row vector of 4 elements. But the answer requires the matrix to be of only 3 element. So, I took the transpose of the Matrix given in the question and made into a 4X3 matix, thus receiving a 3 element row matrix an output for the max() and min() commands. You can certainly reduce the number of lines in the code I posted.
Guillaume
Guillaume 2019 年 6 月 11 日
Rather than transposing matrices, which is expensive (plus you've used the conjugate transpose instead of plain transpose) simply tell min and max which dimension to operate on.
%no tranpose required
maxi = max(A, [], 2); %operate across 2nd dimension
This also has the advantage that the code doesn't break for a Nx1 matrix (yours will).
KETAN PATEL
KETAN PATEL 2019 年 6 月 14 日
Yeah, I was aware that my code will not run in all the scenarios. could you please explain how max(A, [], 2) works? I am new to MATLAB.
Thank you
Guillaume
Guillaume 2019 年 6 月 14 日
Most matlab functions such as max, min, sum, mean, all, etc. will accept as their last argument the dimension across which to work. Thus max(A, [], 2) takes the max across the 2nd dimension (across the columns), sum(A, 2) the sum across the columns, etc.
KETAN PATEL
KETAN PATEL 2019 年 6 月 14 日
Thank you!
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 日

1 投票

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 日

0 投票

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 件のコメント

Guillaume
Guillaume 2019 年 4 月 4 日
What is the error message you get?
Kailash Ramasubramaniam
Kailash Ramasubramaniam 2020 年 5 月 8 日
function [mmr,mmm]=minimax(r)
mmr= [max(r(1,[1:end]))- min(r(1,[1:end])),max(r(2,[1:end]))- min(r(2,[1:end])),...
max(r(3,[1:end]))- min(r(3,[1:end]))];
mmm=max(r(:))-min(r(:));
end
This the code which I wrote for this question. This works fine for matrices till 3 rows,after which it fails. I am new to matlab. Can someone help me to correct this code for random matrices please?
Ronald Grant
Ronald Grant 2020 年 8 月 5 日
編集済み: Ronald Grant 2020 年 8 月 5 日
function [mmr, mmm] = minimax(M)
mmq = max(M,[],2)-min(M,[],2);
mmr = transpose(mmq);
mmm = max(M(:))-min (M(:));
end
try to modify few things in your code, and here you go. nice code btw really help me alot :D
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 日

0 投票

4 件のコメント

Durga Charan
Durga Charan 2019 年 4 月 13 日
you needs to transpose the final results. your function calls in colum. but it is asked as a row vector.
Gregorio Giorgi
Gregorio Giorgi 2019 年 7 月 6 日
Thank you Durga, I was getting the same as Rose, but then I transposed the final results to a row vector like you suggested.
This is how I solved it in the end (there are probably other ways to do it):
function [mmr, mmm] = minimax (M)
mmr_1 = max(M, [], 2) - min(M, [], 2);
mmr = mmr_1'
mmm = max(M, [], 'all') - min(M, [], 'all');
end
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
VIJAY VIKAS MANGENA 2020 年 8 月 13 日
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 日

0 投票

function [mmr, mmm] = minimax(M)
mmr = (max(M,[],2)-min(M,[],2))';
mmm = max(M(:))-min(M(:));
end

3 件のコメント

Muhammad Najeeb khan
Muhammad Najeeb khan 2019 年 6 月 19 日
can you please explain the mmr code.
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 日

0 投票

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
Dorbala sankarshana 2020 年 6 月 23 日
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 日

0 投票

function [mmr,mmm]=minimax(M)
A=(M(:,:))';
mmr=max(A(:,:))-min(A(:,:));
mmm=max(max(A))-min(min(A));
end

1 件のコメント

DGM
DGM 2023 年 2 月 27 日
A(:,:)
is the same as
A

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

Amit Jain
Amit Jain 2020 年 10 月 24 日

0 投票

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

1 件のコメント

DGM
DGM 2023 年 2 月 27 日
A(1:end,1:end)
is the same as
A

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

ANDIE MEDDAUGH
ANDIE MEDDAUGH 2021 年 7 月 7 日
編集済み: DGM 2023 年 2 月 27 日

0 投票

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.

カテゴリ

タグ

質問済み:

2019 年 3 月 22 日

編集済み:

DGM
2023 年 2 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by