How to Use arrayfun with a Multivariate Function of n Variables?

조회 수: 5 (최근 30일)
Kareem Elgindy
Kareem Elgindy 2020년 8월 3일
댓글: Arthur Roué 2020년 8월 4일
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é
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)
end
  댓글 수: 4
Kareem Elgindy
Kareem Elgindy 2020년 8월 3일
OK. But how to calculate the 3xN matrix quickly then. Can we do it without for loops?
Arthur Roué
Arthur Roué 2020년 8월 4일
Can you share the code of your function ?

댓글을 달려면 로그인하십시오.


Souradeep Mukhopadhyay
Souradeep Mukhopadhyay 2020년 8월 3일
You should make 3 different X arrays here X1, X2, X3 and then pass the 3 arrays in function 3 times to compute y 3 times.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by