Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to vectorize this loop

Subject: how to vectorize this loop

From: Mateusz Gos

Date: 19 Apr, 2011 01:12:04

Message: 1 of 4

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

Subject: how to vectorize this loop

From: Roger Stafford

Date: 19 Apr, 2011 02:04:05

Message: 2 of 4

"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
- - - - - - - -
  You should not have the "n = n+1;" line in there. The for-loop with n takes care of advancing n. Also the line "n = 1;" is totally unnecessary since the for-loop will start at n = 1 no matter how you set n in advance.

  As your code stands at present you are taking twenty different mean operations, the first one the mean of the single number 1, then the mean of the two number 1 and 2, then that of 1, 2, and 3, and so forth. Is that what you had in mind? If so, all but the last one of these means from 1 to 20 have been overwritten because you placed them in the same variable R without indexing it with n.

  If all you want is the single mean of the numbers from 1 to 20, then do this:

 R = mean(1:20);

  If you want the twenty different means, do this:

 R = zeros(20,1);
 for n = 1:20
  R(n) = mean(1:n);
 end

Roger Stafford

Subject: how to vectorize this loop

From: Yuri Geshelin

Date: 19 Apr, 2011 13:14:04

Message: 3 of 4

"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

Subject: how to vectorize this loop

From: Steven_Lord

Date: 19 Apr, 2011 13:41:34

Message: 4 of 4



"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:ioiqil$42j$1@fred.mathworks.com...
> "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
> - - - - - - - -
> You should not have the "n = n+1;" line in there. The for-loop with n
> takes care of advancing n. Also the line "n = 1;" is totally unnecessary
> since the for-loop will start at n = 1 no matter how you set n in advance.
>
> As your code stands at present you are taking twenty different mean
> operations, the first one the mean of the single number 1, then the mean
> of the two number 1 and 2, then that of 1, 2, and 3, and so forth. Is
> that what you had in mind? If so, all but the last one of these means
> from 1 to 20 have been overwritten because you placed them in the same
> variable R without indexing it with n.
>
> If all you want is the single mean of the numbers from 1 to 20, then do
> this:
>
> R = mean(1:20);
>
> If you want the twenty different means, do this:
>
> R = zeros(20,1);
> for n = 1:20
> R(n) = mean(1:n);
> end

For this particular problem, since the mean of a vector of values is the sum
of the elements in the vector divided by the number of elements in the
vector, you could use CUMSUM.

% Sample data
measurements = rand(1, 20);
n = 20;

% Engine
R = cumsum(measurements(1:n))./(1:n);

% Spot check the answer
R(5) - mean(measurements(1:5))

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us