how can I create a function with 3 subfunctions?

22 ビュー (過去 30 日間)
Eva Ribera
Eva Ribera 2018 年 4 月 1 日
編集済み: Awanish Kumar singh 2022 年 5 月 8 日
The code needs two anonymous functions, 3 outputs and 2 arrays inputs

回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 4 月 1 日
function [out1, out2, out3] = homework1(in1, in2)
anon1 = @(x) x*2;
anon2 = @(x) x+5;
out1 = subfunction1( anon1(11) );
out2 = subfunction2( anon2(11) );
out3 = subfunction3( anon1(11)*anon2(11) );
function y = subfunction1( x )
y = sin(x);
end
function y - subfunction2( x )
y = coth(x);
end
function y = subfunction3( x )
y = exp(-x);
end
end

Awanish Kumar singh
Awanish Kumar singh 2022 年 5 月 8 日
編集済み: Awanish Kumar singh 2022 年 5 月 8 日
%I create a function with 3 subfunction
function [avg, med] = newstats(u) % Primary function
% NEWSTATS Find mean and median with internal functions.
n = length(u);
avg = mean(u, n);
med = median(u, n);
function a = mean(v, n) % Subfunction
% Calculate average.
a = sum(v)/n;
function m = median(v, n) % Subfunction
% Calculate median.
w = sort(v);
if rem(n, 2) == 1
m = w((n+1) / 2);
else
m = (w(n/2) + w(n/2+1)) / 2;
end

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by