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

4 Ansichten (letzte 30 Tage)
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))

Antworten (1)

Jan
Jan am 11 Nov. 2017
Bearbeitet: Jan am 11 Nov. 2017
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 Kommentare
Stef
Stef am 11 Nov. 2017
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 am 12 Nov. 2017
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by