我如何将y指定为Matlab中2个函数的向量? [英] How do I designate y as a vector of 2 functions in Matlab?

查看:142
本文介绍了我如何将y指定为Matlab中2个函数的向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个for循环来运行我的程序并调出我创建的函数程序。我已经成功地让它运行,但现在我想再简化一步,通过将y定义为两个函数的向量而不是y1和y2来缩短我的程序。我试着用里面的函数创建一个2单元阵列,但这是一个失败的努力。这是我的。

I am trying to create a for loop to run through my program and call out a function program I have created. I have been successful in getting it to run but now I would like to simplify it one more step and shorten my program by defining y as a vector of two functions instead of y1 and y2. I tried creating a 2 cell array with the functions inside but this is a failed effort. Here is what I have.

f =cell(size(a));
f(1) =[@(t)  4*t];
f(2) =[@(t)   20+(5-t).^2];

任何有关将单元2组织成矢量的建议?

Any advice on organizing these 2 into a vector for a single input of y?

推荐答案

如果您使用单元格数组,则会发生什么情况:

What if you use a cell array instead:

f =cell(2,1);

f(1) ={@(t)  4*t}; %// Note the curly braces
f(2) ={@(t)   20+(5-t).^2};

或者使用以下内容,这更直观地使用单元格数组(谢谢@rayryeng!)。

OR using the following, which is more intuitive using cell arrays (thanks @rayryeng!). Assign the content of the cell with curly braces instead of doing the opposite as above.

f{1} =@(t)  4*t;
f{2} =@(t)   20+(5-t).^2;

celldisp(f)

f{1} =

    @(t)4*t



f{2} =

    @(t)20+(5-t).^2

这篇关于我如何将y指定为Matlab中2个函数的向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆