can someone help me loop this??
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Roger Stafford
2013 年 9 月 30 日
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.
その他の回答 (2 件)
Walter Roberson
2013 年 9 月 29 日
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!