フィルターのクリア

What does this code do?

1 回表示 (過去 30 日間)
V
V 2014 年 5 月 17 日
コメント済み: V 2014 年 5 月 17 日
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.

採用された回答

Star Strider
Star Strider 2014 年 5 月 17 日
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 件のコメント
Star Strider
Star Strider 2014 年 5 月 17 日
編集済み: Star Strider 2014 年 5 月 17 日
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 2014 年 5 月 17 日
Thank you. And thanksto Jan Simon as well.

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2014 年 5 月 17 日
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.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by