What does this code do?
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
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.
추가 답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!