【问题标题】:Why does my function display only one output variable?为什么我的函数只显示一个输出变量?
【发布时间】:2019-02-14 05:59:26
【问题描述】:

我正在学习使用 matlab。在下面的代码中,我想应用 mldivide 函数https://www.mathworks.com/help/matlab/ref/mldivide.html 但是,我对函数如何处理输出有点困惑。根据文档https://www.mathworks.com/help/matlab/ref/function.html,当我在命令窗口中调用我的函数时,我应该向我吐出 out1、out2 和 out3。但只显示 out1。为什么?

function [out1, out2, out3] = testSystem(in1, in2, in3)

b = [in1; in2; in3];

A = [2, 1, 1; 
    -1, 1, -1;
    1,  2, 3;];

x = A\b;

disp(x);
out1 = x(1,1);
out2 = x(2,1);
out3 = x(3,1);

end
>> testSystem(2,3,-10)
     3
     1
    -5


ans =

     3

【问题讨论】:

标签: matlab


【解决方案1】:

因为你没有分配输出。因此,该函数仅返回第一个输出。 如果你打电话给[out1, out2, out3] = testSystem(2,3,-10),它应该做你想做的。

【讨论】:

  • 我认为当我声明 out1、out2、out3 变量时,我正在分配输出。感谢您告诉我如何正确执行此操作。
  • @Nalt 澄清一下:通过将函数定义为function [out1, out2, out3] = testSystem(...),您声明它可以产生三个输出。但是要将三个输出实际分配给三个变量,您需要调用具有三个输出的函数,就像@shamalaia 所说的那样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 2018-11-23
  • 1970-01-01
  • 2017-12-16
  • 1970-01-01
  • 2021-06-06
相关资源
最近更新 更多