Conditional Calculations or Loop

조회 수: 1 (최근 30일)
Brian
Brian 2011년 11월 1일
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