Bjartmar
Bjartmar

Reputation: 163

Rounding numbers

I'm trying to make Matlab round numbers to numbers on certain interval. I have a large vector and I need to round them up to or down.

% Input
A = [1 2 3 4 5 6 7 8 9 10]

% Interval of allowed numbers.
dE = 3;

% Rounding
B = round(A,dE); % Does not work like I desire.

% Desired output
B == [0 3 3 3 6 6 6 9 9 9 ]

Upvotes: 0

Views: 108

Answers (2)

Stewie Griffin
Stewie Griffin

Reputation: 14939

You can't round integers, so you need to divide it by the desired interval, dE. After rounding, you can multiply it by dE again.

A = [1 2 3 4 5 6 7 8 9 10]
% Interval of allowed numbers.
dE = 3;    
B = dE * round(A / dE)
B =
     0     3     3     3     6     6     6     9     9     9

Upvotes: 2

Anton
Anton

Reputation: 4684

% Input
A = [1 2 3 4 5 6 7 8 9 10];

% Interval of allowed numbers.
dE = 3;

% Rounding
B = round(A/dE)*dE; 

Upvotes: 3

Related Questions