How to use just one output out of a function with several outputs in an equation?

4 ビュー (過去 30 日間)
Stef
Stef 2017 年 11 月 11 日
コメント済み: Jan 2017 年 11 月 12 日
I have a function with three outputs depending on x. I want to use one of these three say B(x) in an equation:
function [ A,B,C ] = f( x )
Equation=1+x+B(x)
How do I have to code the equation? what I want to have is something like this (does not work):
Equation=1+x+f(B(x))

回答 (1 件)

Jan
Jan 2017 年 11 月 11 日
編集済み: Jan 2017 年 11 月 11 日
You have to do this in two lines:
[~, B] = f(x);
Equation = 1 + x + B(x);
Of course you could write a function, which extracts a certain output only, but this is less clear:
function X = SelectOutput(N, Fcn, varargin)
[Out{1:N}] = Fcn(varargin{:});
X = Out{N};
end
Then:
Equation = 1 + x + SelectOutput(2, f(x));
I don't think, that this is an advantage.
  2 件のコメント
Stef
Stef 2017 年 11 月 11 日
But why should I write a function then? (I have the task to do so) Because then I could just do it like this:
b=@(x) 3*x
Jan
Jan 2017 年 11 月 12 日
I cannot follow you. This code is unclear already:
function [ A,B,C ] = f( x )
Equation=1+x+B(x)
Why does "B" appear in the output, when it is used inside the function? Where do A and C come from?
Then "b=@(x) 3*x" seems to be completely unrelated. Please start from scratch and explain, which problem you try to solve. Append this information by editing the original question.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by