Conditional Calculations or Loop

1 次查看(过去 30 天)
I've got a question about how Matlab works with conditional formulas. I have a dataset imported that contains tickers, dates, and returns (each as separate arrays). What I'd like to calculate eventually are large rolling period medians applying various filters to my datasets. To get started, I'm wondering if someone can point me in the right direction.
What I'd like to do is calculate the product of my (MyReturns) array for each ticker in my tickers array(MyTickers) and a certain time period (MyDates). Once I have all the product’s calculated, I’d like to calculate the median of that dataset and write that value to a separate array for future use and move on to another time period. My Data looks like this (see below)-
Say I want to calculate the Product of MyReturns for MyDates = 1/1 and 1/2. Then find the median of the returns and store that value. How would I go about writing a formula or a loop to do that calculation within Matlab?
Thanks a lot! Brian
MyTickers MyDates MyReturns
GOOG 1/1/2011 1.01
GOOG 1/2/2011 1.02
GOOG 1/3/2011 0.99
GOOG 1/4/2011 0.98
GOOG 1/5/2011 1
AAPL 1/1/2011 1.03
AAPL 1/2/2011 1.01
AAPL 1/3/2011 1.02
AAPL 1/4/2011 0.99
AAPL 1/5/2011 0.98
IBM 1/1/2011 1
IBM 1/2/2011 1.01
IBM 1/3/2011 1.01
IBM 1/4/2011 0.99
IBM 1/5/2011 0.99

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-11-1
All you need is to properly index and select the data from the array. I don't understand what do you mean by "Product" though.
MyTickers={'GOOG','GOOG','GOOG','AAPL','AAPL','IBM','IBM'}';
MyDates={'1/1/2011','1/2/2011','1/3/2011','1/1/2011','1/2/2011','1/2/2011','1/3/2011'}';
MyReturns=[1.01 1.02 0.99 1.03 1.04 0.8 0.7];
MyNumDates=datenum(MyDates);
Index1=strcmp(MyTickers,'GOOG');
Index2=MyNumDates>=datenum('1/1/2011') & MyNumDates<datenum('1/3/2011');
Index=Index1 & Index2;
SelectData=MyReturns(Index)
prod(SelectData)
  4 个评论
Brian
Brian 2011-11-2
I think you've given me enough to get started. However, the code that you gave me suggests that I only have a few dates and a few securities. I actually have a few years worth of dates and a few thousand securities. I will need to programatically move those each of those lists. How would I go about doing that?
Fangjun Jiang
Fangjun Jiang 2011-11-2
I don't understand. In the example, MyTickers etc. only has a few elements. If you have hundreds of elements, the code will work the same and return whatever data that meets the criteria.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Categorical Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by