Using functions to create a function

9 次查看(过去 30 天)
3nore
3nore 2013-3-25
I want to create 2 functions called ADX and ADXR (ADXR uses ADX to calculate itself). This is the function for ADX-
function [ adx ] = adx( t, p )
adx(i) = [adx(i-t)*(p-t)+DX)]/p
end
Is this the correct way to program the function for ADXR?
function [ adxr ] = adxr( p )
adxr(i) = [adx(i) + adx(i-p)]/2
end
Or does it need the input arguments of adx -"adx(t,p)"? If so, how do I tell matlab I want the previous adx value and not the current value?
function [ adxr ] = adxr( p )
adxr(i) = [adx(t, p) + adx(i-p)(t,p)]/2
end
  2 个评论
Walter Roberson
Walter Roberson 2013-3-25
Do not name a variable the same thing as your function name: there is too much chance of accidentally getting recursion.
Image Analyst
Image Analyst 2013-3-25
Maybe he's an old Visual Basic programmer, where it does it that way.

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2013-3-25
In your line
adx(i) = [adx(i-t)*(p-t)+DX)]/p
end
because "i" is not a parameter to the function and is not a variable you initialize, "i" is going to default to sqrt(-1) . If your code managed to get anywhere, it would fail because you cannot index an array at sqrt(-1)

Image Analyst
Image Analyst 2013-3-25
Maybe something like this:
function adxrOutput = adxr( p, i )
adxrOutput = [adx(i) + adx(i-p)]/2
end
function adxOutput = adx(t, p, i)
adxOutput = [adxOutput(i-t)*(p-t)+DX)]/p
end
I'm not sure I follow what you're trying to do though, and in the adx function, it's still not going to work (even though I made the output variable separate from the function name), but I don't know how to fix it because you haven't said what you want to do.

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by