matlabにおける@の意味はなんでしょうか?

32 views (last 30 days)
Takumi Enomoto
Takumi Enomoto on 7 Jul 2022
Answered: Hernia Baby on 8 Jul 2022
reductionFcn = @(x)x;
上記の構文が例題にあったのですが、右辺の意味がわかりません。
matlabにおける@の意味は何でしょうか?

Accepted Answer

交感神経優位なあかべぇ
Edited: 交感神経優位なあかべぇ on 7 Jul 2022
reductionFcn = @(x)x; の@は無名関数、関数ハンドルなどとよばれ、関数を変数などに格納したりすることができます。
変数に格納した関数ハンドルは、()づけで引数を定義することで定義した関数を呼び出すことができます。
下記のように、使用します。
a = @(x) disp(x);
a(1);
1
b = @() disp(2);
b();
2
  1 Comment
交感神経優位なあかべぇ
ちなみに、下記コマンドを実行すると、ボタンがひとつあるGUIが生成されるのですが、このボタンを押したときに呼び出される関数を定義したりなどの使われ方をします。(下記の場合は、ボタンを押すとdisp(1)が呼び出されます。)
uibutton('ButtonPushedFcn', @(src, event) disp(1));

Sign in to comment.

More Answers (1)

Hernia Baby
Hernia Baby on 8 Jul 2022
■reductionFcn = @(x) x;について
 無名関数、通称ラムダ(LAMBDA)関数における変数の宣言です。
 意味としましては、reductionFcn = @(使う変数) 関数 になります。
■何がうれしいか
 よく使用される数式の関数を作成し、使いまわせることが利点です。
f = @(x,y) x + y
f = function_handle with value:
@(x,y)x+y
f(1,2)
ans = 3
f((1:3),5)
ans = 1×3
6 7 8
■その他
 cellfunのようなものに対しても使用できます。
Fs = 100;
t = (0:1/Fs:3);
T = table2cell(table((1:3)')) % 1~3の変数をcell型にしています
T = 3×1 cell array
{[1]} {[2]} {[3]}
スイープパラメータの設定
C = cellfun(@(x) MyFcn(t,x),T,UniformOutput=false); % cell型のxのみ使用する
まとめて図示
figure
hold on
cellfun(@(x) plot(t,x),C,UniformOutput=false)
自作関数
function y = MyFcn(t,x)
tmp = x.^2;
y = tmp.*sin(2*pi.*x.*t);
end

Categories

Find more on 行列および配列 in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!