why can't I get a vector as an output while using a constant in the function handle ?

3 ビュー (過去 30 日間)
ahmad
ahmad 2014 年 2 月 5 日
コメント済み: Walter Roberson 2014 年 2 月 5 日
I define the following constant function
f=@(x,y)0;
and I define the following vector
z=[1 2; 3 4; 5 6];
I want to evaluate the function f using feval
feval(f,z(:,1),z(:,2));
my question is that why can't I get the output as a vector instead of a scalar ?

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 5 日
編集済み: Azzi Abdelmalek 2014 年 2 月 5 日
Because that's what your function do, the result is always 0, it's independent of your inputs
  5 件のコメント
ahmad
ahmad 2014 年 2 月 5 日
but shouldn't I get the output in the same size as the input ?
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 5 日
編集済み: Azzi Abdelmalek 2014 年 2 月 5 日
I will give you a mathematical example
f(t)=10
The output is always 10, give any value for t, the output will not change
f(0)=10
f(100)=10
The same for your function
f=@(x,y)0
The result is independent of x and y, the result will be always 0. you can connect the size of your output to your input by
f=@(x,y) zeros(size(x))
or
f=@(x,y) x*0

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


Walter Roberson
Walter Roberson 2014 年 2 月 5 日
f = @(x,y) zeros(size(x));
  1 件のコメント
Walter Roberson
Walter Roberson 2014 年 2 月 5 日
If you want to execute a function once for each input in a vector, you can use arrayfun
arrayfun(f, z(:,1), z(:,2))

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

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by