Anonymous Function Array Output

Write a script that contains an anonymous function min_max to find the minimum and the maximum of an array. The output should be returned as a 1D array. The first value contains the minimum, and the second the maximum. The function should make use of internal functions.

6 件のコメント

Geoff Hayes
Geoff Hayes 2017 年 10 月 26 日
Zhuoyi - what have you tried so far? You have posted what appears to be your assignment but have not described what you have attempted. Please clarify which part of the question that you need help with, discussing what you have tried and what is preventing you from moving forward.
Zhuoyi Chen
Zhuoyi Chen 2017 年 10 月 26 日
I tried the following code, but it seems not working.
x = rand(1,10);
min_max = @(x) deal(min(x),max(x))
[a,b] = min_max(x)
OCDER
OCDER 2017 年 10 月 26 日
What is the error message? Your code works for me.
Zhuoyi Chen
Zhuoyi Chen 2017 年 10 月 26 日
Well, it is a question posted by my teacher online, and the website indicated that the following statement....
OCDER
OCDER 2017 年 10 月 26 日
Based on the assert tests, it wants:
y = min_max(x)
where
y(1) = minimum of x
y(2) = maximum of x
Zhuoyi Chen
Zhuoyi Chen 2017 年 10 月 26 日
Yeah, but I don't know how to output an array like that, I mean, using the anonymous function.

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

回答 (2 件)

Honglei Chen
Honglei Chen 2017 年 10 月 26 日

1 投票

You are returning two separate outputs, the instruction says "The output should be returned as a 1D array"
HTH

2 件のコメント

Zhuoyi Chen
Zhuoyi Chen 2017 年 10 月 26 日
emmmmmmmm... so what should I do then?
Honglei Chen
Honglei Chen 2017 年 10 月 27 日
right now you are using deal to separate the outputs into two. What you need to do is instead of that, putting them into an array. for example, [1 2] will be an array consisting of two elements.

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

Ryan Miller
Ryan Miller 2019 年 2 月 4 日

0 投票

% Your code goes here
min_max = @(A) [min(A), max(A)];
%define random array A
A = rand(1,10);
%display minimum and maximum
disp(min(A));
disp(max(A));

カテゴリ

ヘルプ センター および File ExchangePerformance and Memory についてさらに検索

質問済み:

2017 年 10 月 26 日

回答済み:

2019 年 2 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by