help-octave
[Top][All Lists]
Advanced

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

Re: Removing For Loops


From: Ben Abbott
Subject: Re: Removing For Loops
Date: Tue, 03 Aug 2010 07:29:07 -0400

On Aug 3, 2010, at 7:13 AM, dirac wrote:

> I have been trying to think how to remove a for loop in my code for a while
> and have run out of ideas! Basically I have a vector A and I want to create
> a matrix such that:
> 
> 
> 
> for i = 1:length(A)
> B = [A(i+1)-A(i), (A(i+1)+A(i))/2)]   %this will be two columns, the first
> is the difference and
> end                                            %the second the mean of the
> two elements used for the diff.
> 
> 
> 
> Is there a way to 'vectorize' this or would it be unnecassary to do so?
> Many thanks in advance.
> Martin
> 

Just for tricks, I'll use a row vector.

        A = rand(1,10);
        B = [diff(A)(:), 0.5*(A(1:end-1)+A(2:end))(:)]

Ben


reply via email to

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