How to pass multiple arguments to a function stored in a vector?
조회 수: 25 (최근 30일)
이전 댓글 표시
Hello guys;
I'm making a program for my college assignment and I got stuck in a situation. So, I have a vector X storing x1,x2,x3....xn (i.e. X = [x1,x2,x3,...xn] ) and a function f(x1,x2,x3,...xn). e.g. let f(x1,x2) = sin(x1)+cos(x2) and X = [1,2]. So I want to write a code which accepts the values stored in X (i.e. x1 and x2 in this given example) and use them for function arguments (i.e. f(x1,x2) = f(1,2) = sin(1)+cos(2)).
댓글 수: 2
KALYAN ACHARJYA
2020년 11월 26일
X=[......]
Pass the entire vector
function out_put_arguments=fun1(X)
.........
end
If I don't understand the question, pleas can you provide more with the sample example.
채택된 답변
Stephen23
2020년 11월 27일
편집: Stephen23
2020년 11월 27일
The solution is to use a cell array for the input values, for example:
C = cell(1,N);
for k = 1:N % do NOT use i
str = input(sprintf('Enter variable x%d: ',k),'s');
C{k} = str2double(str);
end
and then when calling any function, use a comma-separated list, e.g. with the first function:
F{1}(C{:})
You could even call all functions using cellfun:
cellfun(@(f)f(C{:}),F)
Read more about comma-separated lists:
댓글 수: 2
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!