How to Use arrayfun with a Multivariate Function of n Variables?
2 ビュー (過去 30 日間)
Suppose f(X) is a function of a vector X = [x1, x2, ..., xn]. How to evaluate the function at a certain range of values, say y1, y2, ..., ym, for the variable xi while keeping all other variables fixed?
To make it simple: Say x1, ..., x59, x61, ..., x100 = 10, and we want to evaluate the function for three different values for x60, say at 1.5, -3, and 12. Can we do this without for loops?
回答 (2 件)
Arthur Roué 2020 年 8 月 3 日
編集済み: Arthur Roué 2020 年 8 月 3 日
arrayfun is generally not faste than a for loop, becaut it has an internal for loop. read this for more details : https://fr.mathworks.com/matlabcentral/answers/324130-is-arrayfun-faster-much-more-than-for-loop
So I reccomend something like
X = [x1 .. xn];
x60 = [1.5, -3, 12];
Y = NaN(1, numel(x60)); % Pre-allocation (not required for small vectors)
for idx = 1:numel(x60)
X(60) = x60(idx)
Y(idx) = yourFun(X)