Borrar filtros
Borrar filtros

What does this code do?

1 visualización (últimos 30 días)
V
V el 17 de Mayo de 2014
Comentada: V el 17 de Mayo de 2014
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
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
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
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

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by