ベクトルの各要素を関数に渡して結果をベクトルで取得したい
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Katsuto Tsujimoto
am 27 Mär. 2020
Kommentiert: Katsuto Tsujimoto
am 27 Mär. 2020
a, b, cの3つの引数を持つ関数myfuncがあったとして、一つの引数だけを変化させたときのmyfuncの挙動を調べたいです。
このとき、すでにあるベクトルの各要素を関数に渡し、ベクトルの形で結果を取得するようにしたいのですが、そのような関数はMatlabにあるでしょうか。
forループを使って書けば次のようになります。
res = null(10);
for i = 1:10
tmp = myfunc(i,2,3);
res(i) = tmp;
end
disp(res)
function G = myfunc(a, b, c)
G = a*b+c;
end
あるいはMATLABではこういう場合forループを使うのが一般的なのでしょうか。
助言いただければ幸いです。
0 Kommentare
Akzeptierte Antwort
michio
am 27 Mär. 2020
arrayfun を使って
res = arrayfun(@(x) myfunc(x,2,3), 1:10)
でもいいですし、そのままベクトルを入力してもOKな形に関数を実装していれば、
res = myfunc(1:10,2,3)
でも同じ結果になります。R2020a で試しましたが、古いバージョンだと2つ目の方法は怒られるかもしれません。
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!