Passing array arguments to an anonymous function
조회 수: 17 (최근 30일)
이전 댓글 표시
I have an anonymous function, for example
f = @(x,y)x.^2.*y.^2
In general for my problem I don't know how many arguments f will have. Nor do I know how many elements each argument will have.
I want to pass in a single element as an argument. If x and y are scalars, I can define z = num2cell(x,y), then write
f(z{:})
But if x and y are vectors this doesn't work. I could write
for ii=1:2; z = { x(ii);y(ii) }; f(z{:}) ;end
but of course I want to avoid the loop.
Is there some efficient way to obtain what I want by writing something like
f(q{:})
where q{:} is a cell array with each cell containing a vector rather than a scalar.
댓글 수: 1
Walter Roberson
2013년 2월 26일
num2cell(x,y) is not the same thing as {x,y} when x and y are scalars. Instead num2cell(x,y) says to use y as the dimension number for the num2cell(x) operation.
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!