Write a function called minmax that takes a two-dimensional array
(matrix) as an input argument (you do not have to check the input argument)
and returns the minimum and the maximum element in the matrix. It
also needs to print out these values to two decimal point precision according
to this example run:
>> [x y] = minmax(randn(20,20))
The minimum of the matrix is -3.00
The maximum of the matrix is 2.71
x =
-2.9962
y =
2.7081

3 件のコメント

Siddharth Varshney
Siddharth Varshney 2021 年 12 月 10 日
function [x, y] = minimax(A)
x = max(A);
y = min(A);
fprintf('The maximum number is %d\n',x(A));
fprintf('The minimum number is %d\n',y(A));
end
This is the code i wrote for this question, but it is showing:-
Index exceeds the number of array elements. Index must not exceed 2.
Error in minimax (line 4)
fprintf('The maximum number is %d\n',x(A));
Can someone spot my mistake ?
Walter Roberson
Walter Roberson 2021 年 12 月 10 日
How does this differ from what the other posts about minmax needed to do?
Rik
Rik 2021 年 12 月 10 日
@Siddharth Varshney Are you planning to hand in the response from Chunru as your homework? Because that would be academic fraud.
It is also not very smart, because if you don't learn to solve the problem yourself, you will have to ask Chunru to do your next assignment as well. He(?) is very helpful, but at some point even he will stop doing your homework for you.
You can respond with images all you like, that isn't going to change my view, and I doubt it will change Walter's view.

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

 採用された回答

Chunru
Chunru 2021 年 12 月 10 日

1 投票

minimax(randn(4,4));
The maximum number is 2.777390e+00 The minimum number is -1.591676e+00
function [x, y] = minimax(A)
x = max(A(:)); % use colon here to find the max of all elements in 2d matrix
y = min(A(:));
fprintf('The maximum number is %d\n',x); % x(A) is wrong
fprintf('The minimum number is %d\n',y);
end

2 件のコメント

Siddharth Varshney
Siddharth Varshney 2021 年 12 月 10 日
thankyou for your help
Walter Roberson
Walter Roberson 2021 年 12 月 10 日
This is a homework question that has an active large thread already

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2021b

タグ

質問済み:

2021 年 12 月 10 日

コメント済み:

Rik
2021 年 12 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by