Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

loop through a matrix and display the number of elements until the same value occcurs based on two columns of the matrix

Asked by Locks on 17 Mar 2013

below you find the data I am reffering to

K PC Price T date r moneyness

1160 1 111.95 0.052055 734506 0.0026425 1.09512931

1165 1 107 0.052055 734506 0.0026425 1.090429185

1170 1 102.1 0.052055 734506 0.0026425 1.085769231

1175 1 97.35 0.052055 734506 0.0026425 1.081148936

1180 1 92.45 0.052055 734506 0.0026425 1.076567797

1185 1 87.6 0.052055 734506 0.0026425 1.072025316

1190 1 82.75 0.052055 734506 0.0026425 1.067521008

1195 1 78 0.052055 734506 0.0026425 1.063054393

1200 1 73.2 0.052055 734506 0.0026425 1.058625

1205 1 68.45 0.052055 734506 0.0026425 1.054232365

1210 1 63.8 0.052055 734506 0.0026425 1.049876033

1215 1 59.15 0.052055 734506 0.0026425 1.045555556

1220 1 54.6 0.052055 734506 0.0026425 1.041270492

1225 1 49.75 0.052055 734506 0.0026425 1.037020408

1230 1 45.7 0.052055 734506 0.0026425 1.032804878

1235 1 41.4 0.052055 734506 0.0026425 1.028623482

1240 1 37.2 0.052055 734506 0.0026425 1.024475806

1245 1 33.15 0.052055 734506 0.0026425 1.020361446

1250 1 29.3 0.052055 734506 0.0026425 1.01628

1255 1 25.55 0.052055 734506 0.0026425 1.012231076

1260 1 22.05 0.052055 734506 0.0026425 1.008214286

1265 1 18.8 0.052055 734506 0.0026425 1.004229249

1270 1 15.75 0.052055 734506 0.0026425 1.000275591

1275 1 13.05 0.052055 734506 0.0026425 0.996352941

1280 1 10.65 0.052055 734506 0.0026425 0.992460938

1285 1 8.55 0.052055 734506 0.0026425 0.988599222

1200 1 70.6 0.049315 734507 0.0026411 1.05624975

1205 1 65.7 0.049315 734507 0.0026411 1.051866971

1210 1 61.25 0.049315 734507 0.0026411 1.047520413

1215 1 56.55 0.049315 734507 0.0026411 1.04320963

1220 1 52.15 0.049315 734507 0.0026411 1.03893418

1225 1 47.55 0.049315 734507 0.0026411 1.034693633

1230 1 43.4 0.049315 734507 0.0026411 1.030487561

1235 1 39.05 0.049315 734507 0.0026411 1.026315547

1240 1 34.85 0.049315 734507 0.0026411 1.022177177

1245 1 31.2 0.049315 734507 0.0026411 1.018072048

1250 1 27.1 0.049315 734507 0.0026411 1.01399976

1255 1 23.75 0.049315 734507 0.0026411 1.00995992

1260 1 20.3 0.049315 734507 0.0026411 1.005952143

1265 1 16.9 0.049315 734507 0.0026411 1.001976047

1270 1 13.4 0.049315 734507 0.0026411 0.99803126

1275 1 11.4 0.049315 734507 0.0026411 0.994117412

1280 1 9.25 0.049315 734507 0.0026411 0.990234141

1285 1 7.2 0.049315 734507 0.0026411 0.986381089

1290 1 5.8 0.049315 734507 0.0026411 0.982557907

1295 1 4.25 0.049315 734507 0.0026411 0.978764247

1300 1 3.3 0.049315 734507 0.0026411 0.974999769

1305 1 2.5 0.049315 734507 0.0026411 0.971264138

1310 1 1.875 0.049315 734507 0.0026411 0.967557023

1315 1 1.15 0.049315 734507 0.0026411 0.963878099

1320 1 1.05 0.049315 734507 0.0026411 0.960227045

1325 1 0.65 0.049315 734507 0.0026411 0.956603547

1330 1 0.55 0.049315 734507 0.0026411 0.953007293

1155 1 116.9 0.12603 734507 0.0029711 1.095887706

1160 1 112.3 0.12603 734507 0.0029711 1.091164052

1165 1 107.55 0.12603 734507 0.0029711 1.086480944

1170 1 103.1 0.12603 734507 0.0029711 1.081837863

1175 1 98.35 0.12603 734507 0.0029711 1.077234298

1180 1 93.95 0.12603 734507 0.0029711 1.072669746

1185 1 89.6 0.12603 734507 0.0029711 1.068143713

1190 1 85.2 0.12603 734507 0.0029711 1.063655714

1195 1 81 0.12603 734507 0.0029711 1.059205272

1200 1 76.6 0.12603 734507 0.0029711 1.054791917

1205 1 72.35 0.12603 734507 0.0029711 1.050415187

1210 1 68.4 0.12603 734507 0.0029711 1.046074628

1215 1 64.55 0.12603 734507 0.0029711 1.041769794

1220 1 60 0.12603 734507 0.0029711 1.037500246

1225 1 56.75 0.12603 734507 0.0029711 1.033265551

1230 1 52.2 0.12603 734507 0.0029711 1.029065285

1235 1 48.6 0.12603 734507 0.0029711 1.024899028

1240 1 45.15 0.12603 734507 0.0029711 1.020766371

1245 1 41.75 0.12603 734507 0.0029711 1.016666908

1250 1 38.65 0.12603 734507 0.0029711 1.01260024

1255 1 34.95 0.12603 734507 0.0029711 1.008565976

1260 1 32.05 0.12603 734507 0.0029711 1.00456373

1265 1 28.7 0.12603 734507 0.0029711 1.000593123

1270 1 25.4 0.12603 734507 0.0029711 0.99665378

1275 1 23.35 0.12603 734507 0.0029711 0.992745333

1280 1 20.4 0.12603 734507 0.0029711 0.988867422

1285 1 18.1 0.12603 734507 0.0029711 0.985019689

1290 1 16.55 0.12603 734507 0.0029711 0.981201783

1295 1 14.1 0.12603 734507 0.0029711 0.977413359

1300 1 12 0.12603 734507 0.0029711 0.973654077

I need to find the element where the first column on the left (K) is the same and additionally the data is obtained from the next date (for example the first date is 734506 and K= 1200, so I am looking for the row in which K=1200 occcurs at day 734507 and so on). For K=1200 on day 734507 I need to row of K=1200 on day 734507. From what I can see it's not possible to compute that based on the two criteria date and K, because for one date there are several K's, but it should be possible for the time to maturity (T). T is computed usind numbers of days outstanding/366 and the difference between T of two options between two weekdays is 1/366. So what I am looking for is a way how I can find for the same strike (K) the option which has one day less time to maturity T (T(1)= T(0)-1/366) the difficulties is, that in the sample there are just trading days that means it's possible that between two datapoints there are three days and no just one. are there ways to do that? at least to know how to get the number of the row in which the next value with the same strike and another criteria which is the same would help me a lot.

thanks!

0 Comments

Locks

Products

No products are associated with this question.

2 Answers

Answer by Cedric Wannaz on 17 Mar 2013
Edited by Cedric Wannaz on 18 Mar 2013
Accepted answer

Assuming this array is stored in variable data, if you wanted to extract all entries where K = 1220, you would do it this way:

 >> data(data(:,1)==1200,:)
 ans =
   1.0e+05 *
    0.0120    0.0000    0.0007    0.0000    7.3451    0.0000    0.0000
    0.0120    0.0000    0.0007    0.0000    7.3451    0.0000    0.0000
    0.0120    0.0000    0.0008    0.0000    7.3451    0.0000    0.0000

If you wanted just prices (column 3), you would get them with

 >> data(data(:,1)==1200,3)
 ans =
      73.2000
      70.6000
      76.6000

If you wanted the date (column 5), this would be

 >> data(data(:,1)==1200,5)
 ans =
      734506
      734507
      734507

So there is no need to use both K and date/time to extract relevant data, unless you really need to know/process data/time specifically.

EDIT: this solution is providing all entries that have same K as the one that you are interested in. Based on the same principle, here is a function that provides you with the next entry with same K as the K found on a row that you specify:

 >> findNext = @(rId) data(rId + find(data(rId+1:end,1) == data(rId,1), 1),:) ;
 >> findNext(9)
 ans =
   1.0e+05 *
    0.0120    0.0000    0.0007    0.0000    7.3451    0.0000    0.0000

which is the entry that you are interested in, meaning the next entry with K=1200 .. if I understand well.

Or simpler, if you just want the rowId:

 >> findNextRow = @(rId) rId + find(data(rId+1:end,1) == data(rId,1), 1) ;
 >> findNextRow(9)
 ans =
    27
 >> findNextRow(1)
 ans =
     55

However, are you sure that you want to treat your data row by row (take one, find next, find next, find next, ..) when it is likely that you could find a way to treat the whole set of entries in a vector, one-shot way?

38 Comments

Cedric Wannaz on 4 Apr 2013

You can add a third input parameter to the function that I proposed, that would allow you to pass the 1/365 or 1/366 factor to the function.

To understand what we are doing, you have to play with simpler cases, e.g a matrix

 >> A = randi(8, 10, 3) ;  A(:,3) = A(:,3) + rand(10, 1)-0.5
 A =
    8.0000    6.0000    3.0085
    8.0000    6.0000    6.0108
    5.0000    6.0000    7.3176
    1.0000    4.0000    1.2948
    2.0000    5.0000    8.1443
    3.0000    3.0000    6.8786
    7.0000    6.0000    4.3116
    1.0000    2.0000    4.0328
    1.0000    6.0000    3.8507
    2.0000    2.0000    3.4390

and try to extract rows based on e.g. values from the first and second columns, using conditional operators and logical indexing. Then you can try a little more complicated, and include conditions on the 3rd column (not integer). Example

 >> id = A(:,1) > 6
 id =
     1
     1
     0
     0
     0
     0
     1
     0
     0
     0

what does this mean, what can you do with that, indexing? counting? other? what happens when we evaluate A(id,1) or A(id,2) or A(id,3) or or A(id,:)? do we have all rows at once or do we have to loop?

I don't understand what you want to do with " we stop the calculation as soon as the value in column 6 is not equal to 734507". As far as I understood, most time series were across multiple days ..?

About tutorials, there are plenty available online. Whichever you take, what you want at this stage is to improve your knowledge about matrix/vector indexing (subscript, linear, logical) and conditional operators. You already have a goal with your project, which is a great driver; you can quite easily understand plot options, integration, interpolation, etc, if you have the mathematical background, but my guess is that at this point what slows you down is data manipulation. That's why I mention indexing. There would also be knowing differences between arrays, cell arrays, structs, struct arrays, datasets, and how to index/use them, how to reshape/cat arrays, and how to perform operations on each element of data structures (arrayfun, cellfun, structfun). You could also learn about function handles, and anonymous functions. I gave you the keywords, now it's up to you to find information. A good source is http://www.mathworks.com/help/pdf_doc/allpdf.html in MATLAB's section.. these aren't tutorials per se, but there is a lot of information in these PDFs (e.g Primer, Mathematics, Data Analysis, Programming).

Locks on 6 Apr 2013

a colllegue of mine was able to help me, based on the code you provided me and it seems as it's working now. however, thanks a lot for your time and your input, it was really usefull and I will have a look at those information

Cedric Wannaz on 6 Apr 2013

You're welcome! Please accept the answer if my original answer + 19 quite long comments helped.

Cedric Wannaz
Answer by Locks on 17 Mar 2013

sorry, I think I didn't make myself quite clear because all startet with another question here in the forum.

I am looking for a vector where I can see the number of the next element which corresponds to the one in the same line in the matrix I entered above.

for example when looking at the 9th line:

1200 1 73.2 0.052055 734506 0.0026425 1.0586250

I am looking for the rownumber of the next element with 1) K (first column)=1200 and T(4th colum) = 0.052055-1/366

if there are weekends, there will be three days in between that would mean: K (first column)=1200 and T(4th colum) = 0.052055-3/366

I hope this way it's clearer what I am looking for

4 Comments

Locks on 24 Mar 2013

I can see to plot, but in the end we're looking for a vector which tells us, in which row the next option with the same properties (same strike, call/put, and same time to maturity, except one/two/three days less) and this code doesn't give us that or am I mistaken?

Cedric Wannaz on 24 Mar 2013

See my comment below the answer above.

Locks on 31 Mar 2013

I sent you the data. for K=1200 (the option in the 9th row, the following rows contains the same option: row 196 (day 734507) row 391 (day 734508) row 604 (day 734509) row 795 (day 734510) row 988 (day 734513 -> probably weekends in between) row 1181 (day 734514)

as far as I can see, thats it

time to matruity at day 734514 is 0.030137, which corresponds to 11 days

the command you send me does give me just 20 numbers as an output, but I also try to work out a solution with the information you provided to me so far

Locks

Contact us