20 views (last 30 days)

%{

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');

Walter Roberson
on 2 Aug 2020 at 16:47

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.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573898-can-anyone-explain-me-why-m-is-taken-in-the-following-code#comment_957478

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573898-can-anyone-explain-me-why-m-is-taken-in-the-following-code#comment_957478

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573898-can-anyone-explain-me-why-m-is-taken-in-the-following-code#comment_957490

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573898-can-anyone-explain-me-why-m-is-taken-in-the-following-code#comment_957490

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573898-can-anyone-explain-me-why-m-is-taken-in-the-following-code#comment_958093

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573898-can-anyone-explain-me-why-m-is-taken-in-the-following-code#comment_958093

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573898-can-anyone-explain-me-why-m-is-taken-in-the-following-code#comment_958096

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/573898-can-anyone-explain-me-why-m-is-taken-in-the-following-code#comment_958096

Sign in to comment.