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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by