can someone help me loop this??

1 visualizzazione (ultimi 30 giorni)
Jeff
Jeff il 29 Set 2013
Modificato: Jeff il 2 Ott 2013
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

Risposta accettata

Roger Stafford
Roger Stafford il 30 Set 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.
  1 Commento
Jeff
Jeff il 30 Set 2013
this is looking much cleaner; let me try it out and get back to you. Thanks for your suggestions.

Accedi per commentare.

Più risposte (2)

Walter Roberson
Walter Roberson il 29 Set 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.
  2 Commenti
Jeff
Jeff il 30 Set 2013
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 il 30 Set 2013
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

Accedi per commentare.


Jeff
Jeff il 2 Ott 2013
Modificato: Jeff il 2 Ott 2013
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.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by