Can anyone explain me why M' is taken in the following code?

조회 수: 42 (최근 30일)
Aniket Bordekar
Aniket Bordekar 2020년 8월 1일
댓글: Stephen23 2024년 4월 20일
%{
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
%}
%Code :
function [mmr, mmm] = minimax(M)
mmr = max(M') - min(M');
mmm = max(M, [], 'all') - min(M, [], 'all');
  댓글 수: 5
Aniket Bordekar
Aniket Bordekar 2020년 8월 2일
Thanks @WalterRoberson

댓글을 달려면 로그인하십시오.

답변 (5개)

Walter Roberson
Walter Roberson 2020년 8월 2일
By default, min() and max() operate along the first non-scalar dimension. If you have a 2D array, m x n, with m and n both not 1, then that means that min() or max() of the array would produce a 1 x n output -- it has operated along columns, producing one result for each column.
Now suppose you transpose the m x n array to become n x m, with the rows becoming columns and the columns becoming rows, and you min() or max that. You will get a 1 x m result -- one result for each of what were originally rows.
Thus, min(M') is one way of producing a minimum for each row (but it has a problem if the data is complex-valued.)
More clear and robust is to use the syntax min(M,[],2) to process dimension #2 specifically, producing one result for each row.

Shubham Shah
Shubham Shah 2022년 12월 22일
function [mmr,mmm]= minimax(M)
mmt = [max(M,[],2)-min(M,[],2)]
mmr = mmt'
mmm = max(M, [], 'all') - min(M, [], 'all')
end
  댓글 수: 2
Shubham Shah
Shubham Shah 2022년 12월 22일
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
Stephen23
Stephen23 2023년 10월 28일
편집: Stephen23 2023년 10월 28일
Nice, but why the superfluous square brackets and no semi-colons?

댓글을 달려면 로그인하십시오.


Abdul Rafay
Abdul Rafay 2023년 10월 28일
function [mmr,mmm] = minimax(A)
mmr = max(A')-min(A');
a = A(:);
mmm = max(a)-min(a);
end
  댓글 수: 1
Stephen23
Stephen23 2023년 10월 28일
편집: Stephen23 2023년 10월 28일
Fails for any column vector:
[adr,dom] = minimax([1;2;3])
adr = 2
dom = 2
function [mmr,mmm] = minimax(A)
mmr = max(A')-min(A');
a = A(:);
mmm = max(a)-min(a);
end
Tip for the future: read the thread thoroughly before posting anything new:

댓글을 달려면 로그인하십시오.


Japhet Kyarukamba
Japhet Kyarukamba 2024년 4월 20일
편집: Japhet Kyarukamba 2024년 4월 20일
% function // editor window
function [mmr, mmm] = minimax(A)
mmr = max(A, [], 2) - min(A, [], 2); % Compute maximum and minimum along rows
a = A(:); % Reshape A into a column vector to find the absolute difference over the entire matrix
mmm = max(a)-min(a); % Find the maximum and minimum values across the entire matrix
end
% Code to call your function? // command window
[mmr, mmm] = minimax([1:3;4:6;7:9])
mmr = 3x1
2 2 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mmm = 8
% Generate a random matrix of size 1x3 with integers between 1 and 100
random_matrix = randi([1, 100], 1, 3)
random_matrix = 1x3
12 80 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Japhet Kyarukamba
Japhet Kyarukamba 2024년 4월 20일
Alternatively,
% function // editor window
function [mmr, mmm] = minimax(M)
% Transpose M to work on each row, then calculate the differences.
mmr = max(M') - min(M'); % Calculate the absolute differences between maximum and minimum elements in each row.
mmm = max(M, [], 'all') - min(M, [], 'all') % Calculate the absolute difference between maximum and minimum elements in the entire matrix.
end
% Code to call your function? // command window
[mmr, mmm] = minimax([1:3;4:6;7:9])
mmm = 8
mmr = 1x3
2 2 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mmm = 8
% Generate a random matrix of size 1x3 with integers between 1 and 100
random_matrix = randi([1, 100], 1, 3)
random_matrix = 1x3
89 34 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  댓글 수: 1
Stephen23
Stephen23 2024년 4월 20일
Fails for any column vector:
[adr,dom] = minimax([1;2;3])
adr = 2
dom = 2
function [mmr, mmm] = minimax(M)
% Transpose M to work on each row, then calculate the differences.
mmr = max(M') - min(M'); % Calculate the absolute differences between maximum and minimum elements in each row.
mmm = max(M, [], 'all') - min(M, [], 'all'); % Calculate the absolute difference between maximum and minimum elements in the entire matrix.
end
Tip for the future: read the thread thoroughly before posting anything new:

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by