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:
rounding

Subject: rounding

From: Peta

Date: 21 May, 2010 06:19:05

Message: 1 of 3

Hi,

I dont know if I'm just missing it but as far as I can tell you can only round to whole integers in Matlab. I need to round to the nearest .5, for example:

[25.3046; 24.9537; 24.6958; 24.5486]
rounded to:
[25.5; 25; 25; 24.5]

Is there such a function? Or should I just suck it up and right something that will do it?

Thanks everyone!
Peta
:)

Subject: rounding

From: Walter Roberson

Date: 21 May, 2010 06:29:56

Message: 2 of 3

Peta wrote:

> I dont know if I'm just missing it but as far as I can tell you can only
> round to whole integers in Matlab. I need to round to the nearest .5,
> for example:
>
> [25.3046; 24.9537; 24.6958; 24.5486] rounded to:
> [25.5; 25; 25; 24.5]
>
> Is there such a function? Or should I just suck it up and right
> something that will do it?

There is no provided function that will do it in one step.

RoundX = round(X.*2)./2

Note that your sample answers are incorrect for 24.6958: the nearest 1/2
is 24.5 (a difference of 0.1958) not 25 (a difference of .3042)

Subject: rounding

From: Peta

Date: 23 May, 2010 22:45:05

Message: 3 of 3

Thanks very much for your help. Didn't realise I'd made that mistake, but it was just an example.
Thanks

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