can someone help me loop this??
1 view (last 30 days)
Show older comments
n=4 t=1:21
std_t1 = (((pc(1,:)- MA(1,:)).^2)/(n-1)+...
((pc(2,:)- MA(1,:)).^2)/(n-1)+...
((pc(3,:)- MA(1,:)).^2)/(n-1)+...
((pc(4,:)- MA(1,:)).^2)/(n-1)).^(1/2)
std_t2 = (((pc(2,:)- MA(2,:)).^2)/(n-1)+...
((pc(3,:)- MA(2,:)).^2)/(n-1)+...
((pc(4,:)- MA(2,:)).^2)/(n-1)+...
((pc(5,:)- MA(2,:)).^2)/(n-1)).^(1/2)
std_t3 = (((pc(3,:)- MA(3,:)).^2)/(n-1)+...
((pc(4,:)- MA(3,:)).^2)/(n-1)+...
((pc(5,:)- MA(3,:)).^2)/(n-1)+...
((pc(6,:)- MA(3,:)).^2)/(n-1)).^(1/2)
etc .. say I would like to continue this pattern for a total of 21 time periods
0 Comments
Accepted Answer
Roger Stafford
on 30 Sep 2013
Rather than sweating out a vectorization, it's a lot easier to do it with a single for-loop. Also it may be just as fast or faster.
n = 4;
m = 21;
std_t = zeros(m,size(pc,2));
for k = 1:m
std_t(k,:) = std(pc(k:k+n-1,:)-repmat(MA(k,:),n,1),1);
end
The rows of 'std_t' are your vectors std_t1, vtd_t2, etc. Make sure 'pc' is defined up to m+n-1 rows.
More Answers (2)
Walter Roberson
on 29 Sep 2013
T1 = reshape(pc, 4, size(pc,1)/4, []);
T2 = permute( reshape( repmat(MA,[4 1 1]), size(MA,1), 4, size(MA,2) ), [2 1 3] );
T3 = (T1 - T2).^2;
T4 = squeeze( sum(T3,1) );
T5 = sqrt(T4 ./ (n-1));
Now one of the dimensions of T5 corresponds to the stdx* index (that is, corresponds to the subscript of MA that you used), and the other dimension of T5 corresponds to the second dimension of pc and MA. Provided, that is, that I worked out all the manipulations properly in my head.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!