help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help with vectorization of update rule


From: Søren Hauberg
Subject: Re: help with vectorization of update rule
Date: Thu, 21 Oct 2010 17:34:27 +0200

Hi,

You can do something like this

        ## Problem parameters
        A = 2;
        T = 10;
        x0 = rand (20, 1);
        
        ## Loop implementation
        x = x0;
        for it=1:T
            x(:,it+1)=A*x(:,it);
        endfor
        
        ## Vectorised implementation
        y = repmat (x0, 1, T+1);
        y = repmat (x0, 1, T+1) .* repmat (A.^(0:T), rows (x0), 1);
        
        ## Check (should return 1)
        isequal (x, y)
        
Søren

tor, 21 10 2010 kl. 08:29 -0700, skrev grg:
> Sorry, I am reposting this, because I am not sure if it was seen.
> 
> 
>  Hi there,
> 
> I am working with a system of difference equations that I solve using the
> following (pseudo)code:
> 
> x=nan(s,t);
> for it=1:T
>    x(:,it+1)=A*x(:,it);
> endfor
> 
> where A is a square matrix of dimensions (s,s) and x is a matrix of
> dimensions (s,T)
> 
> My question: is there a way to avoid the for loop and speed up the
> computation?
> It would be relatively straightforward to do it, if A was a scalar, rather
> than a matrix.
> 
> But I have no clue on how to do it when A is a matrix.
> 
> Any input will be greatly appreciated.
> Thanks in advance.
> 
> best regards,
> giorgio 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]