Insert multidimensional array into function handle

조회 수: 3 (최근 30일)
Sinipelto
Sinipelto 2021년 3월 23일
댓글: Sinipelto 2021년 3월 23일
Hi,
I'm trying to solve a symbolic var from a function f(x,y), that should take pairs (x,y) as input.
However, MATLAB does not accept the array as input to a function handle to calculate them as a result array.
My code:
syms c
o = [ c 0 0; c 0 2; c 1 1; c 1 2; c 2 2; c 2 3; c 3 0; c 3 1; c 3 2; c 3 3 ]
f = @(c,x,y) c*(x+y)
eq = sum(arrayfun(f, o)) == 1 % ERROR FROM arrayfun() -> doesnt understand the multidim array input
solve(eq, c)
Gives this error:
"Not enough input arguments.
Error in @(c,x,y)c*(x+y)".
Thanks in advance!

채택된 답변

Steven Lord
Steven Lord 2021년 3월 23일
syms c
o = [ c 0 0; c 0 2; c 1 1; c 1 2; c 2 2; c 2 3; c 3 0; c 3 1; c 3 2; c 3 3 ];
f = @(c,x,y) c.*(x+y); % Use element-wise multiplication
V = f(o(:, 1), o(:, 2), o(:, 3))
V = 
solve(sum(V) == 1, c)
ans = 
  댓글 수: 1
Sinipelto
Sinipelto 2021년 3월 23일
Thank you, this is more elegant solution I was trying to achieve first.

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

추가 답변 (1개)

Sinipelto
Sinipelto 2021년 3월 23일
I solved this myself.
The arrayfun(..) can take multiple arrays as inputs. Thus, I had to split up the arrays to individual single dimensional arrays and then pass all of them into the function, like this:
syms c
oc = [ c c c c c c c c c c ]
ox = [ 0 0 1 1 2 2 3 3 3 3 ]
oy = [ 0 2 1 2 2 3 0 1 2 3 ]
f = @(c,x,y) c*(x+y)
eq = sum(arrayfun(f, oc, ox, oy)) == 1
solve(eq, c)
And we get: ans = 1/34.

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by