• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Matlab Function

Joined
12/16/07
Messages
29
Points
11
Hi All,
I am stuck at a point in writing some code.

Please look at this function Look at this function
-----------------------------------------------------------------------
[FONT=&quot]%% Converting Cumulative "R" Values into Quarterly "Q" Values[/FONT]
[FONT=&quot]% For the first quarter, "R" data and "Q" data are equivalent. For[/FONT]
[FONT=&quot]% subsequent quaters (2, 3, & 4) the "Q" value is equivanet to the[/FONT]
[FONT=&quot]% current quarter's value less the previous quater's value.[/FONT]
[FONT=&quot] 1) [/FONT][FONT=&quot]function[/FONT][FONT=&quot] r = rToQ(obj)[/FONT]
[FONT=&quot] 2) [/FONT][FONT=&quot]if[/FONT][FONT=&quot] obj.freq ~= [/FONT][FONT=&quot]'R'[/FONT][FONT=&quot], error([/FONT][FONT=&quot]'obj frequency bust be "R"'[/FONT][FONT=&quot]); [/FONT][FONT=&quot]end[/FONT][FONT=&quot];[/FONT]
[FONT=&quot] 3) r = ifthen(obj.periods()==1, obj, obj - obj.lag(-1));[/FONT]
[FONT=&quot] 4) r.freq = [/FONT][FONT=&quot]'Q'[/FONT][FONT=&quot];[/FONT]
[FONT=&quot] 5) [/FONT][FONT=&quot]end[/FONT]

-----------------------------------------------------------------------
As you can see the above function is used to change the cumulative data into quarterly data. I am trying to implement an ‘if else’ function
I am trying to create function called “ifthen” in Matlab (it is being called in line 4 in the code above) which checks for the period of the object. If it is 1 then the object remains unchanged. If is not 1 then the obj.lag is subtracted from the original obj.

How should I implement this “ifthen” function so that it returns an object? In other words how do I implement an element wise “ifthen “function on an object?
I tried to do something like this but this does not work on an object

[FONT=&quot]function[/FONT][FONT=&quot] r = ifthis( condition, result1, result2)[/FONT]
[FONT=&quot] error(nargchk(3,3,nargin));[/FONT]
[FONT=&quot]if condition == ([/FONT][FONT=&quot]obj.periods()==1[/FONT])
[FONT=&quot] r = obj;[/FONT]
[FONT=&quot]else[/FONT]
[FONT=&quot] r = obj-obj.lag(-1);[/FONT]
[FONT=&quot]end[/FONT]
[FONT=&quot]end[/FONT]


<o:p></o:p>
<o:p>Any help would be appreciated</o:p>

<o:p></o:p>
<o:p>-Thanks
</o:p>
 
Back
Top