[Scilab-users] vector operation replacing for loop

Collewet Guylaine guylaine.collewet at irstea.fr
Thu Nov 1 13:06:07 CET 2018


Hello,

Could you try this?  I think this gives the same result as with the loop
I think that the matrix mat could be probably built in a smarter way

V=zeros(n,1);
V(1)=Z(1);
expo=1:n-1;
vect=[zeros(1,n-1) r.^(expo-1)];
A=makematrix_toeplitz(vect);
mat=A(n:$,1:n-1);
V(2:$)=(Z(1)*r.^expo)'+sqrt(1-r^2)*mat*Z(2:$)';


Guylaine



-----Message d'origine-----
De : users [mailto:users-bounces at lists.scilab.org] De la part de Heinz
Nabielek
Envoyé : jeudi 1 novembre 2018 09:27
À : Users mailing list for Scilab
Objet : [Scilab-users] vector operation replacing for loop

Sorry, Scilab friends, I am still not fluid with vector operations.
Can someone rewrite the for loop for me into something much more
efficient?

n=1000;
Z=grand(1,n,'nor',0,1);
r=0.9;
V=Z;
for i=2:n;
	V(i)=r*V(i-1)+sqrt(1-r^2)*Z(i);
end;

The transformation generates an autocorrelated (here rho=0.9) normal
distribution V from an uncorrelated normal distribution Z and eventually I
will need it for very much larger n values....

Heinz
_______________________________________________
users mailing list
users at lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users



More information about the users mailing list