Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to vectorize this loop
Date: Tue, 19 Apr 2011 13:14:04 +0000 (UTC)
Organization: Bedford Institute of Oceanography
Lines: 21
Message-ID: <iok1qs$637$1@fred.mathworks.com>
References: <ioinh4$f1g$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-04-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1303218844 6247 172.30.248.35 (19 Apr 2011 13:14:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 19 Apr 2011 13:14:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1149396
Xref: news.mathworks.com comp.soft-sys.matlab:722729

"Mateusz Gos" <webmaster24@wp.pl> wrote in message <ioinh4$f1g$1@fred.mathworks.com>...
> hi,
> i would like to write the following loop in form of a vector, which I am guessing is possible (or not?):
> 
> n = 1;
> for n = 1:20
> R = mean(measurements(1:n));
> n = n+1;
> end
> 
> thanks,
> mat

Hi,

vectorization is also possible:

measurements = rand(1,20);
R = cumsum(measurements)./(1:20);

Yuri