Got Questions? Get Answers.
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:
Simple question about number formatting

Subject: Simple question about number formatting

From: Meagan

Date: 7 Apr, 2010 15:05:25

Message: 1 of 10

Hi all,

I have a pretty simple question. I have a matrix V

V =
            0 0 0.5000 0.5000 0.1250
    0.2000 0.2000 0.2000 0.2000 0.2000
    0.0000 -0.5000 -0.5000 0.0000 -0.3750

and I am trying to assign a variable to each position for example: x1=V(1,1);
When I look at the value of x1, it equals 0 but when I type in z1=V(3,1); I get z1 =
  1.2246e-017. How do I get z1=0?

Thanks for your help.

Subject: Simple question about number formatting

From: us

Date: 7 Apr, 2010 15:07:59

Message: 2 of 10

On Apr 7, 5:05 pm, "Meagan " <meagan.mussel...@l-3com.com> wrote:
> Hi all,
>
> I have a pretty simple question. I have a matrix V
>
> V =
>             0            0    0.5000    0.5000    0.1250
>     0.2000    0.2000    0.2000    0.2000    0.2000
>     0.0000   -0.5000   -0.5000    0.0000   -0.3750
>
> and I am trying to assign a variable to each position for example: x1=V(1,1);
> When I look at the value of x1, it equals 0 but when I type in z1=V(3,1); I get z1 =
>   1.2246e-017. How do I get z1=0?
>
> Thanks for your help.

a hint:

     help round; % <- and siblings...

but, why would you want to do this(?!)...

us

Subject: Simple question about number formatting

From: Matt Fig

Date: 7 Apr, 2010 15:20:28

Message: 3 of 10

Why is x1 a better way to access the value than V(1,1)? At least with the array, the addressing is built in to its very structure! What happens when V is 1000x1000, are you really going to assign one million variables to your workspace?

Subject: Simple question about number formatting

From: Meagan

Date: 7 Apr, 2010 15:31:19

Message: 4 of 10

I don't have to assign it to z1 but it still does not give me a value of 0 even though that is the value in my matrix. How do I format the rounding to give me zero instead of 1.2246e-017? Later in my code I have an if statement that needs that value to be equal to zero.

Subject: Simple question about number formatting

From: us

Date: 7 Apr, 2010 15:40:10

Message: 5 of 10

On Apr 7, 5:31 pm, "Meagan " <meagan.mussel...@l-3com.com> wrote:
> I don't have to assign it to z1 but it still does not give me a value of 0 even though that is the value in my matrix. How do I format the rounding to give me zero instead of 1.2246e-017?  Later in my code I have an if statement that needs that value to be equal to zero.

well... did you actually look at what you were told above(!?)...
or are you just puking out another piece of thread...

us

Subject: Simple question about number formatting

From: Mark Shore

Date: 7 Apr, 2010 15:49:09

Message: 6 of 10

"Meagan " <meagan.musselman@l-3com.com> wrote in message <hpi8g7$ng8$1@fred.mathworks.com>...
> I don't have to assign it to z1 but it still does not give me a value of 0 even though that is the value in my matrix. How do I format the rounding to give me zero instead of 1.2246e-017? Later in my code I have an if statement that needs that value to be equal to zero.

Using relational operators (in particular equal to) is bad practice with floating point numbers for reasons you are now becoming aware of. Search for some of the many detailed discussions in the forums.

see also help eps

Subject: Simple question about number formatting

From: Loren Shure

Date: 7 Apr, 2010 16:06:56

Message: 7 of 10

In article <hpi6vl$rd9$1@fred.mathworks.com>, meagan.musselman@l-
3com.com says...
> Hi all,
>
> I have a pretty simple question. I have a matrix V
>
> V =
> 0 0 0.5000 0.5000 0.1250
> 0.2000 0.2000 0.2000 0.2000 0.2000
> 0.0000 -0.5000 -0.5000 0.0000 -0.3750
>
> and I am trying to assign a variable to each position for example: x1=V(1,1);
> When I look at the value of x1, it equals 0 but when I type in z1=V(3,1); I get z1 =
> 1.2246e-017. How do I get z1=0?
>
> Thanks for your help.
>

You need to be clear if you want to change the value or the display.
For display, look at the help for format, disp, or sprintf. For values,
look at round and friends.

--
Loren
http://blogs.mathworks.com/loren
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Simple question about number formatting

From: Mark Shore

Date: 7 Apr, 2010 16:30:21

Message: 8 of 10

Loren Shure <loren.shure@mathworks.com> wrote in message
snip
> For display, look at the help for format, disp, or sprintf. For values,
> look at round and friends.
>
> --
> Loren

"friends"? Is this a typo, or some arcane function I know nothing about? (Please - no jokes...)

Subject: Simple question about number formatting

From: Roger Stafford

Date: 7 Apr, 2010 16:35:41

Message: 9 of 10

"Meagan " <meagan.musselman@l-3com.com> wrote in message <hpi8g7$ng8$1@fred.mathworks.com>...
> I don't have to assign it to z1 but it still does not give me a value of 0 even though that is the value in my matrix. How do I format the rounding to give me zero instead of 1.2246e-017? Later in my code I have an if statement that needs that value to be equal to zero.
-----------------
  What people are trying to say to you, Meagan, is that you are dead wrong when you say "it [referring to v(3,1)] still does not give me a value of 0 even though that is the value in my matrix". The value of V(3,1) is *not* actually zero, as is shown when you displayed it by itself by way of z1. The latter is the display you should have the greater confidence in.

  In the display of V as a whole, other values in it were in the range between 1 and 0.1 and the format was set at four decimal places, so the nearest value it could show for the v(3,1) entry given that restriction was "0.0000". This only means that it must lie somewhere between zero and 0.00005, (which in fact you found to be true.) Notice however that entries that were exactly zero such as V(1,1) were shown as "0", not "0.0000". Displaying "0.0000" rather than "0" is matlab's way of telling you that the corresponding entry is not precisely zero. It's the best that it can do given the constraints of displaying format for an entire array of values.

Roger Stafford

Subject: Simple question about number formatting

From: Loren Shure

Date: 8 Apr, 2010 14:29:26

Message: 10 of 10

In article <hpibut$n12$1@fred.mathworks.com>, mshore@magmageosciences.ca
says...
> Loren Shure <loren.shure@mathworks.com> wrote in message
> snip
> > For display, look at the help for format, disp, or sprintf. For values,
> > look at round and friends.
> >
> > --
> > Loren
>
> "friends"? Is this a typo, or some arcane function I know nothing about? (Please - no jokes...)
>

I meant friends of round - like fix, ceil, etc.

--
Loren
http://blogs.mathworks.com/loren
http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

No tags are associated with 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