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:
colon operator vs for loop for equally-spaced elements

Subject: colon operator vs for loop for equally-spaced elements

From: Michael

Date: 25 Mar, 2009 00:04:02

Message: 1 of 5

Hi,

I'm not sure of the proper terminology here, so I hope you can understand.

if I create two vectors like so:

vcolon = 0:0.05:100;

for i=0:2000
 vfor(i+1) = i * 0.05;
end


then I take the difference between the two, ther are some elements with non-zero difference. These differences are less than epsilon. This seems like a machine precision or otherwise floating-point calc issue.

My question is, what does the '0:0.05:100' do differently than the for loop?

Thanks,
Michael

Subject: colon operator vs for loop for equally-spaced elements

From: Matt Fig

Date: 25 Mar, 2009 01:46:01

Message: 2 of 5

http://www.mathworks.com/support/solutions/data/1-4FLI96.html?solution=1-4FLI96





kauoapo eannYmh tamlcM'aoigat o'o@eee a hituopbv.c E:lbyh

Subject: colon operator vs for loop for equally-spaced elements

From: Roger Stafford

Date: 25 Mar, 2009 01:47:01

Message: 3 of 5

"Michael" <michael@circular-logic.com> wrote in message <gqbsdi$2vb$1@fred.mathworks.com>...
> Hi,
>
> I'm not sure of the proper terminology here, so I hope you can understand.
>
> if I create two vectors like so:
>
> vcolon = 0:0.05:100;
>
> for i=0:2000
> vfor(i+1) = i * 0.05;
> end
>
>
> then I take the difference between the two, ther are some elements with non-zero difference. These differences are less than epsilon. This seems like a machine precision or otherwise floating-point calc issue.
>
> My question is, what does the '0:0.05:100' do differently than the for loop?
>
> Thanks,
> Michael

  Yes it is a binary floating point accuracy issue. The number .05 cannot be represented exactly in binary because in reduced form the fraction's denominator has a factor of 5 which is not a power of 2. Whatever error there is in its nearest binary approximation will cause the error in i*.05 to grow larger as i increases, so it is highly unlikely that your vfor(2000) would round to exactly 100. On the other hand, it seems pretty clear that matlab arranges things in the colon operator logic for a:b:c so that at least at the endpoints there is no error if a+b*i is reasonably close to c. Clearly other unspecified adjustments are made along the line also. It would be interesting to make a study of these to see if one could penetrate Mathworks' innermost secrets in the colon operator.

Roger Stafford

Subject: colon operator vs for loop for equally-spaced elements

From: Matt Fig

Date: 25 Mar, 2009 02:40:19

Message: 4 of 5

vcolon = 0:0.05:100;

% Now do a for loop from both ends.
vforadd2 = zeros(1,2001);
vforadd2(end) = 100;

for ii = 1:1000
 vforadd2(2001-ii) = 100 - (ii)*.05;
end

for ii = 2:1000
 vforadd2(ii) = 0 + (ii-1)*.05;
end

% See how different the results.
max(abs(vforadd2-vcolon))




cWdXtatdWZt`tnVBd{i5^td/jjZVXVVVkbttbV]aZt#ie^dV]]{:dZecNi\

Subject: colon operator vs for loop for equally-spaced elements

From: Michael

Date: 25 Mar, 2009 19:13:02

Message: 5 of 5

Hi,

Thanks for the replies. Matt, your method looks good. It explains why my test only showed differences in the 2nd half of the vector.

Do you know that this is Matlab's method or are you speculating?

Cheers

"Matt Fig" <spamanon@yahoo.com> wrote in message <gqc5ij$228$1@fred.mathworks.com>...
> vcolon = 0:0.05:100;
>
> % Now do a for loop from both ends.
> vforadd2 = zeros(1,2001);
> vforadd2(end) = 100;
>
> for ii = 1:1000
> vforadd2(2001-ii) = 100 - (ii)*.05;
> end
>
> for ii = 2:1000
> vforadd2(ii) = 0 + (ii-1)*.05;
> end
>
> % See how different the results.
> max(abs(vforadd2-vcolon))
>
>
>
>
> cWdXtatdWZt`tnVBd{i5^td/jjZVXVVVkbttbV]aZt#ie^dV]]{:dZecNi\

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