What does this code do?

Hi there,
I am picking up on someone else's code and I am having trouble understanding the meaning of the following lines:
ereom(index,:) = [eom(index) sum(ev(ev(:,1) >= ...
bom(index) & ev(:,1) <= eom(index), 2))];
Where index is a single number, eom is a matrix with dates, bom is another a matrix with dates and ev is a (n by 2) matrix with dates on the first column and prices of a stock on the second column.

 Respuesta aceptada

Star Strider
Star Strider el 17 de Mayo de 2014

1 voto

It creates each row of ereom as index increases. ( I assume here that index is a scalar and not a vector. ) It gets eom(index) as the first column, the sum of the dates of ev (since the first column of ev are the dates) if ev(:,1) >= bom(index) and if the date in |ev(:,1) <= eom(index). The complete sum( ..., 2) indicates by the 2 that the sum is across the columns of ev, producing a column vector, or more likely a scalar, since ereom(index) is a row vector for every scalar value of index. That’s how I read it, anyway.

6 comentarios

V
V el 17 de Mayo de 2014
Hi. I believe you are almost right.
However the code does not sum the dates. Instead it makes the if as you were saying and sums across column 2.
Why does this code has a if statement implied?
Star Strider
Star Strider el 17 de Mayo de 2014
Yes, the implied if is termed ‘logical indexing’ and is quite useful, even if it occasionally makes code difficult to read (as it does here).
I misstated. You are correct in that it does not sum across the dates, but ev(:,2), the prices. It is testing using the dates.
V
V el 17 de Mayo de 2014
Editada: Image Analyst el 17 de Mayo de 2014
Thank you.
I just do not understand the syntax.
sum(matrix(criteria,dim)) ?
Sum elements of matrix, across dimension dim, given criteria.
Am I right?
Image Analyst
Image Analyst el 17 de Mayo de 2014
No, that is not correct. I think that the code you have is way too complicated - it's just not maintainable without someone scratching their head for hours. I really really prefer Jan's suggestion of breaking this down into digestible bite-sized chunks that you can understand. Much easier to comprehend than a massive, cryptic one-liner . Compactness does have its drawbacks.
Take a look at what Jan Simon did and you will understand everything.
Star Strider
Star Strider el 17 de Mayo de 2014
Editada: Star Strider el 17 de Mayo de 2014
My pleasure!
I agree.
The same code could be made more compact than Jan’s ‘trace’ (quite useful for diagnostics) but the line you quoted is very difficult to read. I spent some time — and did some parallel experiments — to be sure I got my explanation as correct as possible.
There’s nothing wrong with logical indexing, but the test criteria, and perhaps even the argument to the sum function, should have been set up before the line you referred to that uses them. That way, they could be checked and traced. The computer has to go through the same steps anyway, so avoiding long, confusing statements in favor a few shorter, clearer ones is good programming practice. It also makes it significantly easier to find errors.
V
V el 17 de Mayo de 2014
Thank you. And thanksto Jan Simon as well.

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 17 de Mayo de 2014

1 voto

Set a breakpoint into this line and start the function again. When this line is reached, split this line into small parts:
t1 = eom(index)
t2 = ev(:,1) >= bom(index)
t3 = ev(:,1) <= eom(index)
t4 = t2 & t3
t5 = ev(t4)
t6 = sum(t5, 2)
ereom(index,:) = t6
This way helps to examine all long worm-like commands.

Categorías

Etiquetas

Preguntada:

V
V
el 17 de Mayo de 2014

Comentada:

V
V
el 17 de Mayo de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by