How to determine the size of an array of functions??

3 ビュー (過去 30 日間)
Timothy Corley
Timothy Corley 2017 年 11 月 27 日
コメント済み: Timothy Corley 2017 年 11 月 27 日
F = @(x1,x2) [(4*x1^2-20*x1+x2^2/4+8) ; (x1*x2/2+2*x1-5*x2+8)]
J = @(x1,x2) [(8*x1-20) (x2/2) ; (x2/2+2) (x1/5-5)]
x0 = [ 1 2 3 4 ];
tol = 10^-5;
A = nargin(F);
B = nargin(J);
I would like to find the size of F & J because i will use those as inputs to my function and they will not be known. Something like:
[C D]=size(F)
[G H]=size(J)
would be excellent if it worked. At first I thought matlab didn't like doing the anonymous matrix thing I'm trying, but it evaluates the functions just fine if given an input like:
F(0,0)
will output:
[8 ; 8]
as it should.
So i tried evaluating the functions with 'A' and 'B' number of zeros, but didn't have much luck in figuring that out as an array is not what the functions input.
Any help would be greatly appreciated!
  3 件のコメント
Timothy Corley
Timothy Corley 2017 年 11 月 27 日
You're right, my apologies. The function is intended to work only when x1,x2,...xn are scalar values, not matrices or vectors. So the size of the output will be equal to the size of the function matrix, 2x1 for F and 2x2 for J in this case. And those are the values I'd like to retrieve
Timothy Corley
Timothy Corley 2017 年 11 月 27 日
編集済み: Timothy Corley 2017 年 11 月 27 日
Should mention here as well that x1,x2,...xn are intended as scalar values. So the size I want to retrieve is equal to the size of the matrix in function form, 2x1 for F and 2x2 for J in this case.
Tried to cheat and run it this way:
F = @(x1,x2) [(4*x1.^2-20*x1+x2.^2/4+8) ; (x1.*x2/2+2*x1-5*x2+8)];
J = @(x1,x2,x3) [(8*x1-20) (x2/2) ; (x2/2+2) (x1/5-5)];
A = nargin(F);
B = nargin(J);
C = zeros(1,A);
D = sprintf('%.0f,', C);
D = D(1:end-1);
D
% F(D)
The output of D is
ans =
'0,0'
if I could evaluate the function at (0,0,...,0) then the size function would do the job just fine, but the function call doesn't like that hahaha, maybe a technicality that can be avoided?

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2017 年 11 月 27 日
編集済み: Stephen23 2017 年 11 月 27 日
There is no general solution because the size of the output can depend on the inputs. Consider:
>> fun = @(x)x+2;
>> fun(1)
ans = 3
>> fun(1:5)
ans =
3 4 5 6 7
>> foo = @(x)zeros(1,x);
>> foo(2)
ans =
0 0
>> foo(7)
ans =
0 0 0 0 0 0 0
Even then it will only return the size for those given inputs. Consider:
>> baz = @(x,y)[x;y];
>> baz(3,2)
ans =
3
2
>> baz(3:6,2:5)
ans =
3 4 5 6
2 3 4 5
>> baz([1;2;3],4)
ans =
1
2
3
4
What size would you describe baz as returning?
The only robust solution is to call the functions with the inputs of the required size and measure the output size:
>> J = @(x1,x2) [(8*x1-20) (x2/2) ; (x2/2+2) (x1/5-5)];
>> size(J(0,0))
ans =
2 2
>> size(J(1:3,1:3))
ans =
2 6
Note how the size of the output from your function depends on the inputs!
  3 件のコメント
Stephen23
Stephen23 2017 年 11 月 27 日
編集済み: Stephen23 2017 年 11 月 27 日
Automatically adjusting for the number of input arguments is easy using a comma-separated list:
>> F = @(x1,x2) [(4*x1^2-20*x1+x2^2/4+8) ; (x1*x2/2+2*x1-5*x2+8)];
>> C = repmat({0},1,nargin(F));
>> size(F(C{:}))
ans =
2 1
and
>> J = @(x1,x2) [(8*x1-20) (x2/2) ; (x2/2+2) (x1/5-5)];
>> C = repmat({0},1,nargin(J));
>> size(J(C{:}))
ans =
2 2
Timothy Corley
Timothy Corley 2017 年 11 月 27 日
Exactly what I wanted!! Thanks a ton mate!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by