can someone help me loop this??

조회 수: 1 (최근 30일)
Jeff
Jeff 2013년 9월 29일
편집: Jeff 2013년 10월 2일
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

채택된 답변

Roger Stafford
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.
  댓글 수: 1
Jeff
Jeff 2013년 9월 30일
this is looking much cleaner; let me try it out and get back to you. Thanks for your suggestions.

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

추가 답변 (2개)

Walter Roberson
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.
  댓글 수: 2
Jeff
Jeff 2013년 9월 30일
Hi Walter, tried to run your first line but got this error
>> T1 = reshape(pc, 4, size(pc,1)/4, []); Error using reshape Size arguments must be real integers.
This is probably due to me using the filter function to generate a 4 quarter moving average: a = 1; b = [1/4 1/4 1/4 1/4]; MA = filter(b, a, pc); MA = MA(4:end-1,:);
Could you propose another way??
Jeff
Jeff 2013년 9월 30일
Hi Walter, just noticed that there was a small error in my example; the row should advance by 1 for variable pc in each block of 4; i.e t1 uses rows 1,2,3,4 t2 uses rows 2,3,4,5$ t3 uses rows 3,4,5,6 etc

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


Jeff
Jeff 2013년 10월 2일
편집: Jeff 2013년 10월 2일
Hi Roger,
this is bang on however I can't completely confirm because I screwed up in my example. The PC matrix is longer then the MA matrix so I ended up getting different results then when I painfully calculated it manually... blah blah blah, HOWEVER i realized that all i essentially need is the std() --i wasn't aware this was the sample std formula - applied on my pc matrix using your code
n = 4; m = 21; std_t = zeros(m,size(pc,2));
for k = 1:m
std_t(k,:) = std(pc(k:k+n-1,:));
end
and this DEFINITELY WORKS!!! Thanks plenty man for dressing up my horrible programming skills.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by