Is there a way of getting this done more efficiently ?
Show older comments
I have the following code and it is taking a very long time to go through it... I dont think its an infinite loop but it is as follows:
Y = zeros(1069,30658);
D1 = LagOp({0,1,1,1},'Lags',[0,1,2,1]);
for n = 2:30658;
for j = 2:1063
if filter(D1,Ret((D1.Degree + j),n),'Initial',Ret(2:D1.Degree,n)) < 0;
Y(j+1,n) = -1*Ret(j+1,n);
else
Y(j+1,n)=Ret(j+1,n) ;
end
end
end
Basically I want to flip the sign of the current element in the matrix if the previous 3 elements before it add up to being less than 0. Otherwise to leave it alone. Could it be the ... else statement causing the trouble here ?
Thanks,
6 Comments
Walter Roberson
on 20 Jan 2016
To check: once you have flipped the sign on a value, it is that new value that will be included in the calculations for the next 3 elements?
I ask because the alternative is much faster, where it is the old value that is used for the sum -- that could be done relatively easily. Your current code does not use the updated element.
Putsandcalls
on 20 Jan 2016
Edited: Putsandcalls
on 20 Jan 2016
Walter Roberson
on 20 Jan 2016
Edited: Walter Roberson
on 20 Jan 2016
Nothing to do with your lag structure. Going by your "basically" description,
Y(:,n) = Ret(:,n);
t = conv(Y(:,n), [1 1 1], 'valid');
mask = [false(3,1); t(1:end-1)<0];
Y(mask,n) = -Y(mask,n);
Half of this code is edge case for not processing the first 3 items because there would not be 3 prior values to sum for them.
The for loop equivalent to the above is
Y(:,n) = Ret(:,n);
for j = 4 : size(Y,1)
if sum(Ret(j-3:j-1)) < 0 %looking at R
Y(j,n) = -Y(j,n);
end
end
This corresponds to the fact that in your existing code, you are always looking at Ret (unchanged) as you move on, so any change you make to Y does not affect your later choice.
Now compare:
Y(:,n) = Ret(:,n);
for j = 4 : size(Y,1)
if sum(Y(j-3:j-1)) < 0 %looking at Y
Y(j,n) = -Y(j,n);
end
end
In this slightly different version, if you change the sign of (say) Y(8) then when you move on to Y(9) it would be the changed Y(8) that you would be adding to Y(6) and Y(7)
For example, consider -1 -2 -3 -7 -5 . Examine the position with the -7 . The sum of the -1 -2 -3 is negative so you change the sign of the -7 leading to -1 -2 -3 +7 -5 . Now when you move on to look at the -5 position, do you add the values that were originally there, -2 -3 -7, and get a negative value so you flip -5 to +5? Or do you look at the changed value, -2 -3 +7, get a positive sum, and so leave the -5 as -5 ?
The vectorized version with conv works on the basis of what the original values were. Is that what you would want?
I have not tried to work out what those lags of yours are doing.
Putsandcalls
on 20 Jan 2016
Walter Roberson
on 20 Jan 2016
Some kinds of filters are implemented using convolutions.
Putsandcalls
on 20 Jan 2016
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!