Arrayfun with multiple variable?

조회 수: 29 (최근 30일)
MementoMori
MementoMori 2022년 4월 19일
편집: Stephen23 2022년 4월 19일
Hi, probably this question has already received answers but what I found don't solve my problem.
I know that I have a function that requires one input i can use arrayfun like this:
A=arrayfun(@functionExample, input1);
But if I have a function that requires more than one input how can I do?
Thanks

답변 (2개)

Voss
Voss 2022년 4월 19일
Two inputs:
A=arrayfun(@(x,y)functionExample(x,y), input1, input2);
Three inputs:
A=arrayfun(@(x,y,z)functionExample(x,y,z), input1, input2, input3);
And so on.
  댓글 수: 3
MementoMori
MementoMori 2022년 4월 19일
Is this caused by varargin in the function "functionExample"
Stephen23
Stephen23 2022년 4월 19일
It is not required to use anonymous functions, a simple function handle works with any number of inputs:
V = arrayfun(@max,[1,2,3],[0,9,0])
V = 1×3
1 9 3

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


Stephen23
Stephen23 2022년 4월 19일
편집: Stephen23 2022년 4월 19일
"But if I have a function that requires more than one input how can I do?"
ARRAYFUN already works with any number of input arrays:
fnh = @(varargin)sum([varargin{:}]); % test function, any number of inputs
arrayfun(fnh,[1,2,3])
ans = 1×3
1 2 3
arrayfun(fnh,[1,2,3],[4,5,6])
ans = 1×3
5 7 9
arrayfun(fnh,[1,2,3],[4,5,6],[7,8,9])
ans = 1×3
12 15 18

카테고리

Help CenterFile Exchange에서 GPU Computing in MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by