how to store vector-valued function value into a single vector variable

4 ビュー (過去 30 日間)
HB
HB 2012 年 1 月 8 日
I've defined a vector-valued function "myvec" in file "myvec.m" like this:
function [a b c]=myvec(x)
a=x+1;
b=x+2;
c=x+3;
end
What syntax do I enter on the command line to get the output vector value of this function stored into a single vector variable "u"?
Note: I tried e.g. to enter on the command line
u=myvec(7)
But this only stores a single scalar value into "u". It results in:
u=
8
i.e., only the 1st element of the actual vector value of myvec(7) gets stored into "u".
What I want instead is to get all 3 elements of myvec(x) stored into "u", so that, e.g.,
u = [8 9 10] for myvec(7).
Can this be done with a single assignment statement, i.e. using something like
u=.... ?
Or
u(:)=.... ?
Or
u(1:3) = .... ?

採用された回答

Robert Cumming
Robert Cumming 2012 年 1 月 8 日
[u(1) u(2) u(3)] = myvec(7);
or a better way is to change your function so that it outputs a vector rather than 3 scalars.
  2 件のコメント
HB
HB 2012 年 1 月 8 日
Thanks! So, I guess re-defining the function as
function a=myvec(x)
a(1)=x+1;
a(2)=x+2;
a(3)=x+3;
end
will work. If I now execute
u = myvec(7)
then the whole vector gets stored into u:
u=
8 9 10
Robert Cumming
Robert Cumming 2012 年 1 月 8 日
exactly

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by