Hi Everyone,
I have some code I want to make faster, and therefore I want to delete my for loops. I have a matrix, TTF_Live_Q, with 3 columns – datenums, bid prices & ask prices. In that matrix I have quarterly prices, so there is only a date/price in every third cell. It could look like this:
0 0 0
0 0 0
735690 26.90 27
0 0 0
0 0 0
735781 28.10 28.20
0 0 0
0 0 0
I want the prices in 2. & 3. column to be repeated 3 times, this I have done with:
for i=1:length(TTF_Live_Q)
if L_Q(i) == 1
TTF_Live_Q(i+1,2:3) = TTF_Live_Q(i,2:3);
TTF_Live_Q(i+2,2:3) = TTF_Live_Q(i,2:3);
end
end
Where L_Q is an index vector. The matrix will now look like:
0 0 0
0 0 0
735690 26.90 27
0 26.90 27
0 26.90 27
735781 28.10 28.20
0 28.10 28.20
0 28.10 28.20
I have tried kron, but that doesn’t work, as there are 0 between the quarterly prices. I am sure you guys have a smarter way to do it.
Next thing I want to improve is the loop for the dates. I need the date number for the beginning of each month. It should look like this:
0 0 0
0 0 0
735690 26.90 27
735720 26.90 27
735751 26.90 27
735781 28.10 28.20
735812 28.10 28.20
735843 28.10 28.20
I have a vector with all the correct dates; Here is the loop that makes the date numbers come into the matrix.
Index_Q = zeros(size(TTF_Live_Q,1),1);
for i=1:3:size(TTF_Live_Q,1)-2
Index_Q(i) = find(TTF_Live_Q(i,1)==Date_Vector);
TTF_Live_Q(i+1,1) = Date_Vector(Index_Q(i)+1);
TTF_Live_Q(i+2,1) = Date_Vector(Index_Q(i)+2);
end
Can you please tell me a smarter way to do it? Thanks a lot!

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 12월 5일
편집: Andrei Bobrov 2013년 12월 5일

0 개 추천

data = [0 0 0
0 0 0
735690 26.90 27
0 0 0
0 0 0
735781 28.10 28.20
0 0 0
0 0 0];
out = data;
l = all(cumsum(data)~=0,2);
d0 = data(l,:);
[y,m,dd] = datevec(d0(1));
out(l,1) = datenum(y,m+(0:size(d0,1)-1)',dd);
d1 = d0(:,2:3);
l1 = d1(:,1)>0;
d2 = d1(l1,:);
out(l,2:3) = d2(cumsum(l1),:);
ADD
data = [0 0 0
0 0 0
735690 26.90 nan
0 0 0
0 0 0
735781 28.10 28.20
0 0 0
0 0 0];
out = data;
l = all(cumsum(data)~=0,2);
d0 = data(l,:);
[y,m,dd] = datevec(d0(1));
out(l,1) = datenum(y,m+(0:size(d0,1)-1)',dd);
d1 = d0(:,2:3);
p = d1(all(d1>0 | isnan(d1),2),:);
ii = find(isnan(p));
p(ii) = p(ii+1);
out(l,2:3) = kron(p,ones(3,1));

댓글 수: 6

Betina Isbak
Betina Isbak 2013년 12월 5일
Hi Andrei,
It works like a charm. However, if there isn't a price for a quarter then the cell will have NaN in it. If the cell has NaN in it, you way will replace the NaN value with the price from the last quarter. I want it to copy the NaN values down like a normal price. Is that possible?
Thank you very much for your help!
Hi!
Do you always want to copy the row (3+n*3) to the two rows below? Then you can write
num = size(data, 1);
copyrows = 3:3:num;
data(copyrows+1, :) = data(copyrows, :);
data(copyrows+2, :) = data(copyrows, :);
Betina Isbak
Betina Isbak 2013년 12월 5일
Hi Simon,
That works great for the prices, but not for the dates. The dates has to change for every row, so I get the correct month for the price.
Thanks!
Andrei Bobrov
Andrei Bobrov 2013년 12월 5일
Hi Betina! See ADD part of my answer.
Simon
Simon 2013년 12월 5일
So, you can mix it with Andrei's answer. Just replace in my answer the ":" with "2:3" in the last two rows.
Betina Isbak
Betina Isbak 2013년 12월 5일
Yeah, that was my thought too.. It works great! Thank you to both of you..

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2013년 12월 3일

댓글:

2013년 12월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by